WordPress程序网站后台全部文章显示字数

文章的字数统计便于我们对文章的效果分析,懒得进去查看文章到底有多长了,下面我就放出代码:

在function.php文件中添加下面的代码(如果文章中有代码,在字数统计上会出现错误)

// 添加文章字数列
add_filter('manage_posts_columns', 'biji_add_column');
function biji_add_column($columns) {
    $columns['biji_wordcount'] = '字数';
    return $columns;
}

// 链接字数到我们的新列
add_action('manage_posts_custom_column', 'biji_display_wordcount', 10, 2);
function biji_display_wordcount($column_name, $post_id) {
    if ($column_name === 'biji_wordcount') {
        // 获取帖子 ID 并将其传递到 get_wordcount 函数
        $biji_wordcount = biji_get_wordcount($post_id);
        echo $biji_wordcount;
    }
}

function biji_get_wordcount($post_id) {
    // 获取帖子,删除任何不必要的标签,然后执行字数统计
    $content = strip_shortcodes(get_post_field('post_content', $post_id));
    $biji_wordcount = mb_strlen(preg_replace('/\s+/', '', strip_tags($content)), 'UTF-8');
    return $biji_wordcount;
}

👋 感谢您的观看!

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享