duaedf6509 2014-12-19 03:50
浏览 43

ACF中继器字段不同的计数

I have a slice of code:

<?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;
?>

What this does it simply count the entries and then stop after 3 - works fine.

However, with the other rows in the repeater field (after 3) how do I get those to also display? Reasoning is these 3 are in a seperate styling div, and all other entries are placed in another div.

However, if I try to repeat this code, nothing shows. So I must need to reset this somehow? I want all repeater rows after 3 to show in a different section.

  • 写回答

1条回答 默认 最新

  • douzi6992 2014-12-19 09:12
    关注

    It looks that your code specifically says not to display more than 3 results! Annotated below.

    $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; ?>
    

    That being said, remove $i = 0, and the following lines:

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

    This will allow ACF to display all the people stored in that metafield.

    However, if you'd like to add a different class to the 4th div and beyond, change the conditional, to remove the break statement, and to wrap the result in a div that identifies it.

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

    Then, you'd have to close it at the end with another check.

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

报告相同问题?

悬赏问题

  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器