关于如何在Wordpress中获取分页帖子当前页面的字数,有什么建议吗?以及一般情况下,如何仅获取分页帖子的当前页面的信息(使用“”分页)。
我基于这个有用的博客帖子做了一个字数统计函数:http://bacsoftwareconsulting.com/blog/index.php/wordpress-cat/how-to-display-word-count-of-wordpress-posts-without-a-plugin/,但它会得到整个帖子的总字数,而不仅仅是当前页面的字数。
非常感谢你的帮助!
发布于 2011-10-01 00:48:37
你将不得不计算页面上所有帖子的字数。假设这是在循环中,您可以定义一个初始化为零的全局变量,然后使用您发布的链接中建议的方法计算每个帖子中显示的字数。
这条线上的一些东西-
$word_count = 0;
if ( have_posts() ) : while ( have_posts() ) : the_post();
global $word_count;
$word_count += str_word_count(strip_tags($post->post_excerpt), 0, ' ');
endwhile;
endif;发布于 2011-10-01 01:35:51
使用$wp_query访问帖子的内容和当前的页码,然后使用PHP的explode()将帖子的内容拆分为多个页面,使用strip_tags()从内容中剥离所有HTML标记,因为它们不算作单词,最后使用str_word_count()计算当前页面的单词数。
function paginated_post_word_count() {
global $wp_query;
// $wp_query->post->post_content is only available during the loop
if( empty( $wp_query->post ) )
return;
// Split the current post's content into an array with the content of each page as an item
$post_pages = explode( "<!--nextpage-->", $wp_query->post->post_content );
// Determine the current page; because the array $post_pages starts with index 0, but pages
// start with 1, we need to subtract 1
$current_page = ( isset( $wp_query->query_vars['page'] ) ? $wp_query->query_vars['page'] : 1 ) - 1;
// Count the words of the current post
$word_count = str_word_count( strip_tags( $post_pages[$current_page] ) );
return $word_count;
}https://stackoverflow.com/questions/7508873
复制相似问题