【问题标题】:PHP - Adding divs to a foreach loop every 4 times, working with $post_objectPHP - 每 4 次将 div 添加到 foreach 循环中,使用 $post_object
【发布时间】:2013-12-22 00:01:32
【问题描述】:

我正在构建一个 wordpress 页面(基于 quark,使用 ACF),使用 foreach(get_field('exhibitions') as $post_object) 显示来自其他帖子的信息

我希望包含来自其他帖子的信息的“col grid_3_of_12-divs”在 div 中分组,每个 div 中有四个。与此问题的作者类似的问题:Is there an easy way to do 4 at a time in a php foreach loop

我已经尝试过使用 array_chunk,但似乎无法在不弄乱从其他帖子中检索信息的情况下做到这一点。

有人可以帮我解决这个问题吗?

我是一个自学的php初学者,如果我的代码很愚蠢,请见谅..

<?php get_header(); ?>

<div id="primary" class="site-content row clearfix" role="main">

    <div class="col grid_12_of_12">

        <?php while ( have_posts() ) : the_post(); ?>

            <?php foreach(get_field('exhibitions') as $post_object): ?>

                <a href="<?php echo get_permalink($post_object->ID); ?>">
                    <div class="col grid_3_of_12">
                        <h3 class="exhibition title"> <?php echo $post_object->short_title?> </h3>

                        <?php $attachment_id = $post_object->thumbnail;
                        $image_attributes = wp_get_attachment_image_src( $attachment_id, 'full' ); ?>
                        <img src="<?php echo $image_attributes[0]; ?>">

                        <p class="exhibition short desc"> <?php echo $post_object->short_description?> </p>
                    </div>
                </a>

            <?php endforeach; ?>

                <?php content_nav( 'nav-below' ); ?>

        <?php endwhile; ?>

    </div>


</div>

【问题讨论】:

    标签: php wordpress foreach


    【解决方案1】:

    添加一个计数器。然后当计数器在正确的位置时,添加一个关闭或打开的 div。

    <?php get_header(); ?>
    
    <div id="primary" class="site-content row clearfix" role="main">
    
      <div class="col grid_12_of_12">
    
        <?php while ( have_posts() ) : the_post(); ?>
    
            <?php $counter = 0; /* ADD THIS */ ?>
            <?php foreach(get_field('exhibitions') as $post_object): ?>
              <?php if ($counter % 4 == 0): /* ADD THIS */ ?>
                <div class="group-of-4-posts-wrapper">
              <?php endif; ?>
    
                <a href="<?php echo get_permalink($post_object->ID); ?>">
                    <div class="col grid_3_of_12">
                        <h3 class="exhibition title"> <?php echo $post_object->short_title?> </h3>
    
                        <?php $attachment_id = $post_object->thumbnail;
                        $image_attributes = wp_get_attachment_image_src( $attachment_id, 'full' ); ?>
                        <img src="<?php echo $image_attributes[0]; ?>">
    
                        <p class="exhibition short desc"> <?php echo $post_object->short_description?> </p>
                    </div>
                </a>
              <?php if ($counter % 4 == 3): /* ADD THIS */ ?>
                </div>
              <?php endif; ?>
              <?php $counter++ ?>
            <?php endforeach; ?>
            <?php 
              // this closes the div if there is not a number of posts that is evenly
              // divisable by 4, like 11 posts. with 11 posts, the last post would have
              // ($counter % 4 == 3) equal to false, because $counter % 4 would = 2
              // adding this, closes the div, if it was not already closed
              if ($counter % 4 != 0): /* ADD THIS.  */ 
            ?>
              </div>
            <?php endif; ?>
    
    
                <?php content_nav( 'nav-below' ); ?>
    
        <?php endwhile; ?>
    
      </div>
    
    
    </div>
    

    【讨论】:

    • 很好地使用了%(模数)运算符。
    • 我只是在寻找这样的东西。你得到了我的投票,因为柜台如果有 11 个帖子。
    最近更新 更多