を使用$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;
}