将下面代码添加到functions.php
1 //取得文章的阅读次数 2 function post_views($before = ‘点击 ‘, $after = ‘ 次‘, $echo = 1) 3 { 4 global $post; 5 $post_ID = $post->ID; 6 $views = (int)get_post_meta($post_ID, ‘views‘, true); 7 if ($echo) echo $before, number_format($views), $after; 8 else return $views; 9 } 10 function record_visitors() 11 { 12 if (is_singular()) { 13 global $post; 14 $post_ID = $post->ID; 15 if($post_ID) { 16 $post_views = (int)get_post_meta($post_ID, ‘views‘, true); 17 if(!update_post_meta($post_ID, ‘views‘, ($post_views+1))) { 18 add_post_meta($post_ID, ‘views‘, 1, true); 19 } 20 } 21 } 22 } 23 add_action(‘wp_head‘, ‘record_visitors‘);
然后在需要用到的地方加入下面的代码即可
<?php post_views(); ?>