Support » Fixing WordPress » How can I check if is the last post?

  • How can I check if is the last post from the current page?
    I want to display divider between posts so last one won’t be necessary.

Viewing 10 replies - 1 through 10 (of 10 total)
  • esmi


    Forum Moderator

    You could try setting up a posts counter that is incremented within the Loop and then comparing the value of that counter to get_option('posts_per_page').

    get_option(‘posts_per_page’) doesn’t help.
    It’s display the posts per page from wp-admin.



    Forum Moderator

    Yes – that’s the point. Unless of course you’ve amended the Loop using something like query_posts.

    Posts per page: 10
    Going to the last page of posts (2 posts only on that page) it displays 10.

    Ok lets imagine we have 27 posts.., posts per page is set to 10, and you’re viewing the 3rd page (7 posts on this page).

    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; // This will be 3 per the above imaginery results
    $postsppage= get_option('posts_per_page'); // 10 per example
    $anothervar = $paged * $postsppage; // result would be 30.
    $morevar = $anothervar - $wp_query->found_posts; // 30 minus the amount of results (so will equal 3)..
    $endvar =  $postsppage - $morevar; // Will equal 7...

    If you put that alongside a counter you’ll know when you’re on the last post for the page…

    if($endvar == 0 || $yourcountervar == $endvar) {
    // Code for last post on the page
    // $endvar will equal 0 when it's the tenth post on the page, so your counter should catch other cases, like in the example when it's the seventh post..

    .. or something to that degree..

    $wp_query->found_posts will contain the amount of posts found for the query ( total ) (regardless of how many pages they’re spread across). It’s a matter of comparing numbers.

    Of course the above code would be additional to a post counter. If you want some examples of how to count posts aswell, then please say so..

    Thanks for the clear explanation, T31os. As always.
    I thought that I missed a function which return this kinda value.

    You’re welcome.. 🙂

    I have an extension of this question. I’m displaying posts side-by-side in pairs, but on certain pages (category lists, portfolio pages, etc…) there are an odd number of posts that are displayed. This number changes as the categories are added too, so I can’t hard code any numbers in here, but I need to find out when I’m at the last post of a category so it will apply a closing style to that odd post.

    When I started reading this thread i had high hopes that it was the answer, but your count-down doesn’t work for me. Any other ideas?



    Forum Moderator

    as far as i know, this
    $num_of_posts = $wp_query->post_count;
    might return the number of posts on a particular page.

    Worked like a charm! Thanks alchymyth!

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘How can I check if is the last post?’ is closed to new replies.