WordPress.org

Ready to get started?Download WordPress

Forums

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

  1. DLOnline
    Member
    Posted 1 year 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:
    http://pastebin.com/YdgTqH1y

  2. Jonathan Lake
    Member
    Posted 1 year 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. alchymyth
    The Sweeper & Moderator
    Posted 1 year 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)

    http://codex.wordpress.org/Class_Reference/WP_Query

  4. DLOnline
    Member
    Posted 1 year 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:

    $count++

    before the end of the foreach loop

Topic Closed

This topic has been closed to new replies.

About this Topic