我正在尝试创建一个计数器,该计数器将显示一个类别页面的页码,后跟页面计数,其中每页有一篇文章。例如,如果一个类别中有10个职位:1/10
, 2/10
, 等等。我能够使用@PieterGoosen提供的代码显示页码(How to use global post counter in the loop?) 但我很难弄清楚如何显示页数。
Display Count of posts
1 个回复
最合适的回答,由SO网友:TheGentleman 整理而成
你应该可以从相同的$wp_query
对象:
global $wp_query;
$num_pages = $wp_query->found_posts;
$num_pages
将包含与您的类别(或您使用的任何其他标准)匹配的总计数。如果您只是想得到一个可以显示的字符串,那么这个修改后的函数版本就可以了。
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;
}
结束