如果你是用WordPress搭建的网站,你是不是想知道你的文章被多少人点击看过?一些用于统计点击次数的插件可以解决这个问题,例如WP-Postviews等等一系列相关插件,但对于大多数朋友来说,使用这样的插件仅仅需要它的点击量统计功能,其他的一切都是多余的,所以我们今天要推荐的是一个更简单的不用任何插件实现点击数统计的方法。
该Wordpress非插件版post_view是参考willin kan大师的my_visitor插件来写的,特点是刷新一次文章页面就统计一次。
操作很简单,将下面的文章点击量统计代码添加到你主题的functions.php的最后一个 ?> 的前面
/* 访问计数 */ function record_visitors() { if (is_singular()) { global $post; $post_ID = $post->ID; if($post_ID) { $post_views = (int)get_post_meta($post_ID, 'views', true); if(!update_post_meta($post_ID, 'views', ($post_views+1))) { add_post_meta($post_ID, 'views', 1, true); } } } } add_action('wp_head', 'record_visitors'); /// 函数名称:post_views /// 函数作用:取得文章的阅读次数 function post_views($before = '(点击 ', $after = ' 次)', $echo = 1) { global $post; $post_ID = $post->ID; $views = (int)get_post_meta($post_ID, 'views', true); if ($echo) echo $before, number_format($views), $after; else return $views; }
然后在需要显示点击数的地方添加下面的调用代码即可
<?php post_views(' ', ' views'); ?>
完成上面两步,就实现了我们想要的效果~~ 怎么样?简单吧~~~~
下面附带另外两个大家可能会用到的函数:
(1)列出阅读次数最多的5篇文章
/// get_most_viewed_format /// 函数作用:取得阅读最多的文章 function get_most_viewed_format($mode = '', $limit = 10, $show_date = 0, $term_id = 0, $beforetitle= '(', $aftertitle = ')', $beforedate= '(', $afterdate = ')', $beforecount= '(', $aftercount = ')') { global $wpdb, $post; $output = ''; $mode = ($mode == '') ? 'post' : $mode; $type_sql = ($mode != 'both') ? "AND post_type='$mode'" : ''; $term_sql = (is_array($term_id)) ? "AND $wpdb->term_taxonomy.term_id IN (" . join(',', $term_id) . ')' : ($term_id != 0 ? "AND $wpdb->term_taxonomy.term_id = $term_id" : ''); $term_sql.= $term_id ? " AND $wpdb->term_taxonomy.taxonomy != 'link_category'" : ''; $inr_join = $term_id ? "INNER JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id) INNER JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)" : ''; // database query $most_viewed = $wpdb->get_results("SELECT ID, post_date, post_title, (meta_value+0) AS views FROM $wpdb->posts LEFT JOIN $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id) $inr_join WHERE post_status = 'publish' AND post_password = '' $term_sql $type_sql AND meta_key = 'views' GROUP BY ID ORDER BY views DESC LIMIT $limit"); if ($most_viewed) { foreach ($most_viewed as $viewed) { $post_ID = $viewed->ID; $post_views = number_format($viewed->views); $post_title = esc_attr($viewed->post_title); $get_permalink = esc_attr(get_permalink($post_ID)); $output .= "<li>$beforetitle$post_title$aftertitle"; if ($show_date) { $posted = date(get_option('date_format'), strtotime($viewed->post_date)); $output .= "$beforedate $posted $afterdate"; } $output .= "$beforecount $post_views $aftercount</li>"; } } else { $output = "<li>N/A</li>\n"; } echo $output; }
(2)获得全站文章总阅读数:
function get_totalviews($echo = 1) { global $wpdb; $total_views = $wpdb->get_var("SELECT SUM(meta_value+0) FROM $wpdb->postmeta WHERE meta_key = 'views'"); if ($echo) echo $total_views; else return $total_views; }
小二苘 2014/03/13 15:37
好方法,收藏了
Specs 2014/03/14 09:33
@ 好东西大家一起分享~
周小姐‘ 2014/03/28 16:27
学习了
Specs 2014/03/28 17:13
@ 这个不好的地方在于更换主题了,还得重新拷贝这些代码~~ 所以我都干脆用插件了
Zouming 2014/09/01 18:06
好办法,谢谢分享