donglan8256 2013-09-26 19:04
浏览 48
已采纳

获取count变量中的最后一项

So I'm using WordPress and created a variable $post_count to track the number of posts.

Right now I'm using if($post_count == 1) to add a class if it's the first post, which works fine, but I can't figure out how to get the last post.

Would that be possible using just a variable to count posts? Or is there a better way to do this than creating a count variable? Here's my code so far:

if($query->have_posts()): $post_count = 0; ?>
    <div class="image-grid">
        <?php while($query->have_posts()): $post_count++; $query->the_post(); ?>
        <div class="item <?php if($post_count == 1) { ?>first_item<?php 
        } elseif() { ?>last item<?php } ?>">post content here</div>
        <?php endwhile; ?>
    </div>
<?php endif; ?>
  • 写回答

2条回答 默认 最新

  • dow5001 2013-09-26 19:11
    关注

    I believe you can do

    $query->found_posts;
    

    to get the total number of posts. So:

    if($query->found_posts == $post_count)
    

    should work.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?