用于$wp_query
访问帖子的内容和当前页码,然后使用 PHP 将帖子的内容拆分为页面explode()
,使用 去除内容中的所有 HTML 标记strip_tags()
,因为它们不算作词,最后只计算当前的词页面与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;
}