如何在Wordpress中获取分页文章的当前页面信息?
关于如何在Wordpress中获得分页文章当前页面的字数,有什么建议吗?一般来说,如何使用获取关于分页后分页的当前页面的信息 我根据这篇有用的博客文章制作了一个wordcount函数:但这会得到整个文章的总字数,而不仅仅是当前页面的字数如何在Wordpress中获取分页文章的当前页面信息?,wordpress,pagination,word-count,Wordpress,Pagination,Word Count,关于如何在Wordpress中获得分页文章当前页面的字数,有什么建议吗?一般来说,如何使用获取关于分页后分页的当前页面的信息 我根据这篇有用的博客文章制作了一个wordcount函数:但这会得到整个文章的总字数,而不仅仅是当前页面的字数 谢谢你的帮助 你必须计算页面上所有帖子的字数。假设这是在循环中,您可以定义一个初始化为零的全局变量,然后使用您发布的链接中建议的方法计算每篇文章中显示的单词数 这句话的意思- $word_count = 0; if ( have_posts() ) : whi
谢谢你的帮助 你必须计算页面上所有帖子的字数。假设这是在循环中,您可以定义一个初始化为零的全局变量,然后使用您发布的链接中建议的方法计算每篇文章中显示的单词数 这句话的意思-
$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;
使用访问帖子的内容和当前页码,然后使用PHP将帖子的内容拆分为页面,使用从内容中剥离所有HTML标记,因为它们不算作单词,最后只算作当前页面的单词
我认为@alison想要的是,只计算一篇分页文章的一页,而不是每一篇文章的字数。
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;
}