WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] WP_Query – Paginated Visible Post Range – x-y of z Results

[Resolved] WP_Query – Paginated Visible Post Range – x-y of z Results

  • James Bullis
    Participant

    @jmbullis

    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.

Viewing 2 replies - 1 through 2 (of 2 total)
  • 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>";
    James Bullis
    Participant

    @jmbullis

    Worked perfect. Thank you for your help!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘[Resolved] WP_Query – Paginated Visible Post Range – x-y of z Results’ is closed to new replies.