我这个主题默认没有阅读次数统计,所以就自己加了一个,方便也比较简单。我这个是没有插件的,个人觉得插件用的越少越好,不得已的情况下再用插件比较好。
第一步、将下面代码加到主题functions模版文件中:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
//postviews 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模版适当的位置:
1 |
<?php get_post_views($post -> ID); ?> views |