[resolved] Check for last post in loop (4 posts)

  1. DLOnline
    Posted 3 years ago #

    I am trying to check for the last post in a loop.
    I need to add a <hr /> under each recent post but not the last post in the loop.

    In previous themes I have used:
    <?php if( ($wp_query->current_post + 1) < ($wp_query->post_count) ) { ?><div class="clear"><hr /></div><?php } ?>

    Except I doesn't work in this case. I think it has something to do with the fact I am not using a standard wp_query.

    Has anyone had this problem before and know how to get past it?

    My current code can be seen here:

  2. Jonathan Lake
    Posted 3 years ago #

    I wonder if this is something you could solve with CSS alone. Do you think something like this would work?

    post:last-child {
        border-bottom: 1px solid #ccc; (whatever)
  3. Michael
    Forum Moderator
    Posted 3 years ago #

    in the foreach loop, you could use a counter variable and check against count( $lastposts )

    or rewrite the loop with WP_Query(); http://pastebin.com/RvQVqWjg (untested)


  4. DLOnline
    Posted 3 years ago #

    I overcame my problem by adding a

    $count = 0;

    outside my for each.

    I then added this line where I wanted, or didn't want, a <hr /> tag:

    if( ($count + 1) < (count($lastposts)) ) { ?><div class="clear"><hr /></div><?php }

    and then obviously added:


    before the end of the foreach loop

Topic Closed

This topic has been closed to new replies.

About this Topic