WordPress.org

Ready to get started?Download WordPress

Forums

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

  1. jmbullis
    Member
    Posted 3 months 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
    Member
    Posted 3 months 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. jmbullis
    Member
    Posted 3 months ago #

    Worked perfect. Thank you for your help!

Reply

You must log in to post.

About this Topic