这其实并不难。使用2个挂钩可以管理帖子列表中的列:manage_posts_columns
, manage_posts_custom_column
add_filter(\'manage_posts_columns\', \'wordcount_column\');
function wordcount_column($columns) {
$columns[\'wordcount\'] = \'Word count\';
return $columns;
}
add_action(\'manage_posts_custom_column\', \'show_wordcount\');
function show_wordcount($name)
{
global $post;
switch ($name)
{
case \'wordcount\':
$wordcount = YOUR_WORD_COUNT_VALUE
echo $wordcount;
}
}
然后,您只需要在save\\u post到post\\u meta并在列中显示计数后计算单词数。
add_action(\'save_post\', function($post_id, $post, $update) {
$word_count = str_word_count( strip_tags( strip_shortcodes($post->post_content) ) );
update_post_meta($post_id, \'_wordcount\', $word_count);
}, 10, 3);
相关的
gist另一种方法是计算单词数量。
function post_word_count($post_id) {
$content = get_post_field( \'post_content\', $post_id );
$word_count = str_word_count( strip_tags( strip_shortcodes($content) ) );
return $word_count;
}
相关的
gist但我建议只计算一次单词save_post
钩请注意,最好将其保存在post\\u meta中,从underscore
例如:_wordcount