wordpress用代码实现文章阅读次数
有时候,我们希望wordpress文章可以显示阅读次数。
其实就是想要知道有多少人来看过。
那我们可以使用这个代码贴入主题的function.php
[cc lang="php"] //wordpress用代码实现文章阅读次数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'); [/cc] 完成后,再开启主题的single.php加入此代码,调用[cc lang="php"]
次阅读[/cc] 在哪个位置加入?通常是在single.php文章模版的这段前面加入 [cc lang="php"]
即可完成啰!