wordpress非插件添加文章浏览次数统计功能并获取最热文章
引言:此文由子域名转移而来,因为细微强迫症和放弃子域名而不舍得完全丢弃,所以将会逐步第二次转移文章到主域名上来,二者主题(阿里白秀和D8)均来自大前端,追求完美的同时有一丝小懒,主题就不换了,D8主题用起来挺好。
在wordpress主题中有不少都用到了wp-postviews插件,它可以统计文章的浏览次数,但对于一些站长不喜欢用太多的插件,能不用就不用,这也是为了加速wordpress程序,所以有必要分享wordpress非插件添加文章浏览次数统计功能。
另外,如果想下载wp-postviews插件可以直接在wordpress后台安装插件处搜索wp-postviews,即可安装成功。
(点击这里查看原文)
非插件统计文章浏览次数——方法一
1、将以下代码插入function.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');
- 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;
- }
2、在需要添加浏览次数的地方添加如下代码即可。
- <?php post_views(' ', ' 次'); ?>
非插件统计文章浏览次数——方法二
1、同样,将如下代码插入到function.php中。
- function getPostViews($postID){
- $count_key = 'post_views_count';
- $count = get_post_meta($postID, $count_key, true);
- if($count==”){
- delete_post_meta($postID, $count_key);
- add_post_meta($postID, $count_key, '0');
- return " 0 ";
- }
- return $count;
- }
- function setPostViews($postID) {
- $count_key = 'post_views_count';
- $count = get_post_meta($postID, $count_key, true);
- if($count==”){
- $count = 0;
- delete_post_meta($postID, $count_key);
- add_post_meta($postID, $count_key, '0');
- }else{
- $count++;
- update_post_meta($postID, $count_key, $count);
- }
- }
2、功能代码添加好后,我们开始进行统计,在single.php中的 endwhile; endif; 循环前添加如下代码:
- <?php setPostViews(get_the_ID());?>
3、最后就可以在需要统计次数的地方添加如下代码了。
- <?php echo getPostViews(get_the_ID()); ?> 次浏览
获取浏览次数最多的文章
1、将以下代码插入到function.php中。
- 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)" : ”;
-
- $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、在需要显示浏览次数最多的文章列表的地方插入如下代码即可。
- <?php get_most_viewed_format(); ?>