Ready to get started?Download WordPress


How can I check if is the last post? (11 posts)

  1. akis
    Posted 4 years ago #

    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.

  2. esmi
    Forum Moderator
    Posted 4 years ago #

    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').

  3. akis
    Posted 4 years ago #

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

  4. esmi
    Forum Moderator
    Posted 4 years ago #

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

  5. akis
    Posted 4 years ago #

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

  6. Mark / t31os
    Posted 4 years ago #

    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..

  7. akis
    Posted 4 years ago #

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

  8. Mark / t31os
    Posted 4 years ago #

    You're welcome.. :)

  9. rphoto
    Posted 4 years ago #

    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?

  10. alchymyth
    The Sweeper & Moderator
    Posted 4 years ago #

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

  11. rphoto
    Posted 4 years ago #

    Worked like a charm! Thanks alchymyth!

Topic Closed

This topic has been closed to new replies.

About this Topic


No tags yet.