2011-09-22 81 views
0

關於我如何獲得Wordpress中分頁郵件當前頁面字數的任何建議?一般來說,如何獲取關於分頁郵件當前頁面的信息(使用「」分頁)。如何在Wordpress中獲取關於分頁郵件當前頁面的信息?

我做了基於這是很有幫助的博客文章單詞計數功能:http://bacsoftwareconsulting.com/blog/index.php/wordpress-cat/how-to-display-word-count-of-wordpress-posts-without-a-plugin/但讓我總字數爲整個帖子,不是隻在當前頁面的計數。

非常感謝您的幫助!

回答

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; 
+0

我認爲@alison想要的是統計分頁文章「<! - nextpage - >」的一頁,而不是每篇文章。 – anroesti

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; 

} 
相關問題