核心代码:function.php添加如下代码
function get_post_views ($post_id) { $count_key = 'views'; $count = get_post_meta($post_id, $count_key, true); if ($count == '') { delete_post_meta($post_id, $count_key); add_post_meta($post_id, $count_key, '0'); $count = '0'; } echo number_format_i18n($count); } function set_post_views () { global $post; $post_id = $post -> ID; $count_key = 'views'; $count = get_post_meta($post_id, $count_key, true); if (is_single() || is_page()) { if ($count == '') { delete_post_meta($post_id, $count_key); add_post_meta($post_id, $count_key, '0'); } else { update_post_meta($post_id, $count_key, $count + 1); } } } add_action('get_header', 'set_post_views');
调用代码
然后在文章页面single.php的需要显示的位置添加如下代码:
阅读次数 <?php get_post_views($post -> ID); ?>
注意:如果部分博主使用的主题是特殊的主题,那么文章页面应该是loop-single.php,把上面方法里面的single.php替换成loop-single.php即可