[resolved] WP_Query - Paginated Visible Post Range - x-y of z Results (3 posts)

  1. James Bullis
    Posted 2 years ago #

    I am using a WP_Query to display the posts from a custom post type. I am using pagination and limiting the number of posts per page to 6. Everything is working but I want to add some information to the bottom of my visible returned posts.

    If I had 10 total posts returned I want the bottom of page 1 to say 1-6 of 10 results. On page 2, I would want it to say 7-10 of 10 results.

    I have figured out how to use found_posts to display the total number of returned results. so I can get the part "of 10 results". But what I can't figure out is how to display the post numbers and show the first and last of the visible posts.

  2. vtxyzzy
    Posted 2 years ago #

    I think this is what you want:

    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
       $total_posts = $wp_query->found_posts;
       $start_post = ($paged - 1) * $posts_per_page + 1;
       $end_post = min($start_post + $posts_per_page - 1, $total_posts);
       echo "<p>$start_post - $end_post of $total_posts results</p>";
  3. James Bullis
    Posted 2 years ago #

    Worked perfect. Thank you for your help!

Topic Closed

This topic has been closed to new replies.

About this Topic