0

Wordpressでページ付けされた投稿の現在のページの単語数を取得する方法について何か提案はありますか?そして、一般的に、ページ付けされた投稿の現在のページのみに関する情報を取得する方法(「」を使用してページ付けされます)。

私はこの役立つブログ投稿に基づいて単語数関数を作成しました:http://bacsoftwareconsulting.com/blog/index.php/wordpress-cat/how-to-display-word-count-of-wordpress-posts-without- a-plugin /ですが、現在のページだけの数ではなく、投稿全体の合計単語数がわかります。

助けてくれてありがとう!

4

2 に答える 2

0

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

}
于 2011-09-30T17:35:51.480 に答える
0

ページ上のすべての投稿の単語をカウントする必要があります。これがループ内にあると仮定すると、ゼロに初期化されたグローバル変数を定義し、投稿したリンクで提案されている方法を使用して、各投稿に表示される単語をカウントできます。

この行の何か -

$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-09-30T16:48:36.800 に答える