【问题标题】:Advanced Custom Fields. Flexible Content with Gallery subfield高级自定义字段。带有图库子字段的灵活内容
【发布时间】:2015-10-03 07:54:51
【问题描述】:

我正在尝试构建一个灵活的内容,其中包含 4 种可能的布局(描述、主图、画廊 1col 和画廊 2col)。到目前为止,我可以让 Description 和 Hero Image 子字段显示得很好,但 Gallery 子字段不会显示在页面上。不知道我做错了什么。代码如下:

<?php

            while(the_flexible_field("flexible_content")): ?>

                <?php if(get_row_layout() == "description"): // layout: Description ?>

                    <div class="proy-desc">
                        <h2><?php the_sub_field("text"); ?></h2>
                    </div>

                <?php elseif(get_row_layout() == "hero_image"): // layout: Hero Image ?>

                    <div class="proy-img">
                        <?php

                            $image = get_sub_field('image');

                            elseif( !empty($image) ): ?>

                                <img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" />

                        <?php ?>
                    </div>

                <?php elseif(get_row_layout() == "gallery_50p"): // layout: Gallery 50p ?>

                    <div class="gallery">
                        <?php $images = get_sub_field('gallery_image_50p');
                           if( $images ): ?>
                              <ul>
                                <?php foreach( $images as $image ): ?>
                                    <li>
                                        <img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" />
                                    </li>
                                <?php endforeach; ?>
                              </ul>
                        <?php ?>
                    </div>
                    <?php endif; ?>

                <?php elseif(get_row_layout() == "gallery_100p"): // layout: Gallery 50p ?>

                    <div class="gallery">
                        <?php $images = get_sub_field('gallery_image_100p');
                           if( $images ): ?>
                              <ul>
                                <?php foreach( $images as $image ): ?>
                                    <li>
                                        <img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" />
                                    </li>
                                <?php endforeach; ?>
                              </ul>
                        <?php ?>
                    </div>
                    <?php endif; ?>

                <?php endif; ?>

            <?php endwhile; ?>

【问题讨论】:

    标签: php wordpress advanced-custom-fields


    【解决方案1】:

    您没有关闭 if 语句。见下文:

    <?php elseif(get_row_layout() == "gallery_50p"): // layout: Gallery 50p ?>
    
                    <div class="gallery">
                        <?php $images = get_sub_field('gallery_image_50p');
                           if( $images ): ?>
                              <ul>
                                <?php foreach( $images as $image ): ?>
                                    <li>
                                        <img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" />
                                    </li>
                                <?php endforeach; ?>
                              </ul>
                        <?php ?>
                    </div>
                <?php endif; ?>
    

    将您的部分更改为:

    <?php elseif(get_row_layout() == "gallery_50p"): // layout: Gallery 50p ?>
    
                    <div class="gallery">
                        <?php $images = get_sub_field('gallery_image_50p');
                           if( $images ): ?>
                              <ul>
                                <?php foreach( $images as $image ): ?>
                                    <li>
                                        <img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" />
                                    </li>
                                <?php endforeach; ?>
                              </ul>
                        <?php endif; ?>
                    </div>
    

    你的endif;在错误的位置。其他一切看起来都不错,所以试试这个。

    【讨论】:

    • 谢谢@Joe - 试过了,但仍然没有画廊领域的迹象。 “画廊” div 也没有显示,所以我猜它一定是 ADC 灵活字段和画廊布局中的错误。我最终像以前一样使用了单个字段。这样都好:)
    猜你喜欢
    • 2015-07-07
    • 2014-03-11
    • 2015-07-07
    • 1970-01-01
    • 2014-03-23
    • 2015-12-24
    • 1970-01-01
    • 2018-05-31
    • 2019-07-14
    相关资源
    最近更新 更多