如何获取有关 Wordpress 中分页帖子的当前页面的信息?

发布于 2024-12-05 14:27:52 字数 427 浏览 1 评论 0 原文

关于如何获取 Wordpress 中分页帖子当前页面的字数统计,有什么建议吗?一般来说,如何获取仅有关分页帖子当前页面的信息(使用“”分页)。

我根据这篇有用的博客文章制作了一个字数统计函数: http://bacsoftwareconsulting.com/blog/index.php/wordpress-cat/how-to-display-word-count-of-wordpress-posts-without-a-plugin/但这让我得到了整个帖子的总字数,而不仅仅是当前页面的字数。

非常感谢您的帮助!

Any suggestions on how I might get the word count of the current page of a paginated post in Wordpress? And in general, how to get information about only the current page of a paginated post (paginated using "").

I've made a wordcount function based on this helpful blog post: http://bacsoftwareconsulting.com/blog/index.php/wordpress-cat/how-to-display-word-count-of-wordpress-posts-without-a-plugin/ but that gets me the total word count for the entire post, not the count for the current page only.

Thanks much for your help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

素年丶 2024-12-12 14:27:52

使用 $wp_query 访问帖子的内容和当前页码,然后使用 PHP 的 explode(),使用 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;

}

Use $wp_query to access the post's content and the current page number, then split the post's content into the pages using PHP's explode(), strip all HTML-tags from the content using strip_tags(), because they don't count as words and finally count the words of just the current page with 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;

}
匿名的好友 2024-12-12 14:27:52

您必须计算页面上所有帖子的字数。假设这是在循环内部,您可以定义一个初始化为零的全局变量,然后使用您发布的链接中建议的方法计算每个帖子中显示的单词数。

这条线的一些东西 -

$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;

You will have to count words of all the posts on the page. Assuming this is inside the loop, you can define a global variable initialized to zero and then count the words displayed in each post using the method suggested in the link you have posted.

Something on this lines -

$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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文