2011-09-22 37 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; 

} 
相关问题