【问题标题】:ACF Repeater Fields different countsACF 中继器字段不同的计数
【发布时间】:2015-02-17 23:58:51
【问题描述】:

我有一段代码:

<?php
if( have_rows('our_people') ):
$i = 0;
while ( have_rows('our_people') ) : the_row(); ?>
    <?php $i++; ?>
    <?php if( $i > 3 ):
        break; ?>
    <?php endif; ?>

    <a class="people-thumb fancybox-inline" href="#fancybox-<?php echo $i;?>">
                <div class="people-thumb-image" style="width:172px;height:172px;overflow:hidden;background:transparent url(<?php the_sub_field('people_image'); ?>) no-repeat center center; background-size:cover;"></div>
                <div class="people-thumb-details">
                    <h3><?php the_sub_field('people_name_acf'); ?></h3>
                    <p><?php the_sub_field('people_title'); ?></p>
                </div>
    </a>

    <div class="peopleoverlay">
        <div id="fancybox-<?php echo $i;?>">
        <img src="<?php the_sub_field('people_image'); ?>" width="172" style="float:left;margin:0 10px 10px 0;"> <?php the_sub_field('people_details'); ?>
        </div>
    </div>

<?php endwhile;    

 else :

 endif;
?>

这是做什么的,它只是简单地计算条目,然后在 3 之后停止 - 工作正常。

但是,对于转发器字段中的其他行(3 之后),我如何让它们也显示?推理是这 3 个在一个单独的样式 div 中,所有其他条目都放在另一个 div 中。

但是,如果我尝试重复此代码,则没有任何显示。所以我必须以某种方式重置它?我希望 3 之后的所有中继器行显示在不同的部分中。

【问题讨论】:

    标签: php wordpress advanced-custom-fields


    【解决方案1】:

    看起来您的代码明确表示不要显示超过 3 个结果!注释如下。

    $i = 0; // Start a counter
    
    while ( have_rows('our_people') ) : the_row(); ?>   // As long as there are posts to display
    <?php $i++; ?>         // increment the counter
    <?php if( $i > 3 ):    // if the counter is 4 or more...
        break; ?>              // don't execute the code below; jump out of the while loop.
    <?php endif; ?>
    

    话虽如此,删除$i = 0 和以下行:

    <?php $i++; ?>
    <?php if( $i > 3 ):
        break; ?>
    <?php endif; ?>
    

    这将允许 ACF 显示存储在该元字段中的所有人员。

    但是,如果您想向第 4 个 div 及以后添加不同的类,请更改条件,删除 break 语句,并将结果包装在标识它的 div 中。

    <?php $i++; ?>
    <?php if( $i > 3 ): ?>
        <div class="other-style">
    <?php endif; ?>
    

    然后,您必须在最后关闭它并再次检查。

    ...
    <?php if( $i > 3 ): ?>
        </div> <!-- .other-style -->
    <?php endif; ?>
    

    【讨论】:

      猜你喜欢
      • 2020-06-14
      • 2018-04-11
      • 2015-03-30
      • 2019-03-30
      • 2017-01-01
      • 1970-01-01
      • 2014-09-22
      • 2018-07-17
      • 2017-01-24
      相关资源
      最近更新 更多