I'm trying to create a counter that will display the page number followed by the page count of a category page, where there is 1 post per page. For example if there are 10 posts in a category: 1/10
, 2/10
, etc. I was able to display the page number using the code supplied by @PieterGoosen (How to use global post counter in the loop?) but I'm having difficulty figuring out how to display the page count.
Add a comment
|
1 Answer
You should be able to get that number from the same $wp_query
object:
global $wp_query;
$num_pages = $wp_query->found_posts;
$num_pages
will contain the total count that matches your category (or whatever other criteria you're using).
If you're just looking to get a string you can display, this modified version of the function will work.
function get_post_number()
{
global $wp_query;
/*
* Get current page number. Set page 1 to one as get_query_var( 'paged' ) will be 0
*/
$current_page_number = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
/*
* Get the posts_per_page option that is set under "Reading"
*/
$posts_per_page = get_option( 'posts_per_page' );
/*
* Get the current post position in the loop, add 1 because the counter starts at 0
*/
$current_post_position = $wp_query->current_post + 1;
/*
* If this is page one, return the post position as is
*/
if ( $current_page_number == 1 )
return $current_post_position;
/*
* Calculate the post number on paged pages
*/
return ( ( $posts_per_page * ( $current_page_number - 1 ) ) + $current_post_position ). '/' . $wp_query->found_posts;
}
-
Sorry, my php is pretty sketchy. So I add that code to my function. How do I get $num_pages to display in my content.php? Thanks. Commented May 14, 2015 at 17:34
-
-
Yes. I didn't change anything except add the bit you have me. Commented May 14, 2015 at 17:55
-
I see where you're coming from. get_posts_number will have the entire thing I want to show. The part I'm not sure about is what to add to the result in the function. Commented May 14, 2015 at 18:16
-
I've edited the post with a modified version of the function that will return page/total as a string. Commented May 14, 2015 at 19:49