2

我正在尝试将转发器字段添加到灵活的内容行中,但由于某种原因没有输出任何内容。我检查了这些字段,它们看起来是正确的,所以有人可以指出我哪里出错了吗?谢谢

        <?php if(get_row_layout() == "collection_title"): // layout: Collection Title ?>

            <div>
                <h4><?php the_sub_field("collection_title"); ?></h4>
            </div>


        <?php elseif(get_row_layout() == "collection_images"): // layout: Collection Images ?>


                <?php if(get_field('collection_images_grid')): ?>

                    <?php while(has_sub_field('collection_images_grid')): ?>

                        <div class="collections">

                            <span><strong><?php the_sub_field('collection_detail'); ?></strong></span>

                            <a href="<?php the_sub_field('product_link'); ?>">

                                <img src="<?php the_sub_field('collection_image'); ?>"/>

                            </a>

                        </div>

                    <?php endwhile; ?>

                 <?php endif; ?>

        <?php endif; ?>

    <?php endwhile; ?>
4

5 回答 5

0

你的<?php if(get_field('collection_images_grid')): ?>陈述应该是<?php if(get_sub_field('collection_images_grid')): ?>

于 2013-12-12T01:26:39.647 回答
0
            <?php

            // check if the flexible content field has rows of data
            if( have_rows('the_process') ){

            // loop through the rows of data
            while ( have_rows('the_process') ) : the_row();

            if( get_row_layout() == 'content' ){

            ?>
            <h1><?php the_sub_field('headline');?></h1>
            <h2 class="tagLine paddingBottom80"><?php the_sub_field('sub_headline');?></h2>

            <div class="steps clearAfter">

            <?php if(get_sub_field('process_steps')): ?>    
            <?php
            while(has_sub_field('process_steps')): 
            ?>



            <!--Step-->
            <div class="processStep rel boxSizing">

            <img src="images/ph.png" width="200" height="200" class="borderRadius50Perc imageBorder boxSizing" />
            <div class="processBox border1 padding20 clearAfter">
            <div class="third processNumber boxSizing font70 darkBlue">
            <div class="border1 padding20">
            <?php echo $i;?>
            </div>
            </div>
            <div class="twothird  boxSizing processContent">
            <h3><?php the_sub_field('step_headline'); ?></h3>
            <div class="processContentTxt grey">
            <?php the_sub_field('step_content'); ?>
            </div>
            </div>
            </div>
            </div>

            <!--Step-->
            <?php endwhile; ?>
            <?php endif; ?>             




            </div>


            <?php   
            }
            endwhile;
            }
            ?>
于 2014-10-09T06:00:18.130 回答
0

假设您的转发器字段是collection_images_grid,您应该循环使用 的项目have_rows(),如下所示:

<?php if(get_row_layout() == "collection_title"): // layout: Collection Title ?>
  <div>
    <h4><?php the_sub_field("collection_title"); ?></h4>
  </div>
<?php elseif(get_row_layout() == "collection_images"): // layout: Collection Images ?>
  <?php if(have_rows('collection_images_grid')): ?>
    <?php while(have_rows('collection_images_grid')): the_row(); ?>
      <div class="collections">
        <span><strong><?php the_sub_field('collection_detail'); ?></strong></span>
        <a href="<?php the_sub_field('product_link'); ?>">
          <img src="<?php the_sub_field('collection_image'); ?>"/>
        </a>
      </div>
    <?php endwhile; ?>
  <?php endif; ?>
<?php endif; ?>

这实质上是检查灵活内容字段是否有数据行 ( <?php if(have_rows('collection_images_grid')): ?>),然后循环/显示它们 ( <?php while(have_rows('collection_images_grid')): the_row(); ?>)。

有关循环字段的更多详细信息have_rows()https ://www.advancedcustomfields.com/resources/have_rows/

于 2019-05-14T19:22:03.310 回答
0

为了调试页面上的所有高级自定义字段并更好地理解结构,我经常使用以下 PHP 片段:

echo '<pre>';
var_dump(get_fields());
echo '</pre>';

这有助于确保数据可用,并弄清楚如何在嵌套结构中访问它。

于 2020-03-29T07:26:25.753 回答
0

您应该在代码中更改一件事。更改<?php if(get_field('collection_images_grid')): ?><?php if(get_sub_field('collection_images_grid')): ?>,它将起作用!我已经模拟了您的问题,更改为 sub_field 后它可以工作。您的代码将如下所示:

<?php if(get_row_layout() == "collection_title"): // layout: Collection Title ?>

            <div>
                <h4><?php the_sub_field("collection_title"); ?></h4>
            </div>


        <?php elseif(get_row_layout() == "collection_images"): // layout: Collection Images ?>


                <?php if(get_sub_field('collection_images_grid')): ?>

                    <?php while(has_sub_field('collection_images_grid')): ?>

                        <div class="collections">

                            <span><strong><?php the_sub_field('collection_detail'); ?></strong></span>

                            <a href="<?php the_sub_field('product_link'); ?>">

                                <img src="<?php the_sub_field('collection_image'); ?>"/>

                            </a>

                        </div>

                    <?php endwhile; ?>

                 <?php endif; ?>

        <?php endif; ?>

    <?php endwhile; ?>

我在 acf 灵活内容中使用中继器时遇到了一些问题。因此,如果我能在这种情况下说些什么来帮助的话,那就是使用一个变量来打印中继器数组的元素,如下所示:

    <?php if(get_row_layout() == "collection_title"): // layout: Collection Title ?>

                    <div>
                        <h4><?php the_sub_field("collection_title"); ?></h4>
                    </div>


                <?php elseif(get_row_layout() == "collection_images"): // layout: Collection Images ?>

    <?php if(get_sub_field('collection_images_grid')): ?> // considering that collections_images_grid are a repeater 

<?php $collection_image = get_sub_field('collection_images_grid');?>

    <?php echo $collection_image['url']; ?> <?php echo $collection_image['alt']; ?> //Or something that you would like to use [... and than the rest of code]
于 2019-05-14T17:47:10.383 回答