WordPress非插件添加文章浏览次数统计功能

2019年12月12日19:22:28 发表评论 热度273 ℃

WordPress文章浏览次数统计功能是必不可少的,不少主题已经集成该功能,如果你的主题没有集成,你可以使用 WP-Postviews 插件,或者试试本文的代码。

WordPress非插件实现文章浏览次数统计的方法,是DH参考willin kan大师的my_visitor插件来写的,刷新一次文章页面就统计一次,比较简单实用。

非插件统计文章浏览次数

1.在主题的 functions.php文件的最后一个 ?> 前面添加下面的代码:

  1. /* 访问计数 */
  2. function record_visitors()
  3. {
  4.     if (is_singular())
  5.     {
  6.       global $post;
  7.       $post_ID = $post->ID;
  8.       if($post_ID)
  9.       {
  10.           $post_views = (int)get_post_meta($post_ID, 'views', true);
  11.           if(!update_post_meta($post_ID, 'views', ($post_views+1)))
  12.           {
  13.             add_post_meta($post_ID, 'views', 1, true);
  14.           }
  15.       }
  16.     }
  17. }
  18. add_action('wp_head', 'record_visitors');
  19. /// 函数名称:post_views
  20. /// 函数作用:取得文章的阅读次数
  21. function post_views($before = '(点击 ', $after = ' 次)', $echo = 1)
  22. {
  23.   global $post;
  24.   $post_ID = $post->ID;
  25.   $views = (int)get_post_meta($post_ID, 'views', true);
  26.   if ($echoecho $before, number_format($views), $after;
  27.   else return $views;
  28. }

2.在需要显示该统计次数的地方使用下面的代码调用:

  1. 阅读:<?php post_views(' ', ' 次'); ?>

获取浏览次数最多的文章

如果要获取上面的函数统计出来的浏览次数最多的文章,可以在 functions.php文件的最后一个 ?> 前面添加下面的代码:

  1. /// get_most_viewed_format
  2. /// 函数作用:取得阅读最多的文章
  3. function get_most_viewed_format($mode = ''$limit = 10, $show_date = 0, $term_id = 0, $beforetitle= '(', $aftertitle = ')', $beforedate= '(', $afterdate = ')', $beforecount= '(', $aftercount = ')') {
  4.   global $wpdb$post;
  5.   $output = '';
  6.   $mode = ($mode == '') ? 'post' : $mode;
  7.   $type_sql = ($mode != 'both') ? "AND post_type='$mode'" : '';
  8.   $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" : '');
  9.   $term_sql.= $term_id ? " AND $wpdb->term_taxonomy.taxonomy != 'link_category'" : '';
  10.   $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)" : '';
  11.   // database query
  12.   $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");
  13.   if ($most_viewed) {
  14.    foreach ($most_viewed as $viewed) {
  15.     $post_ID    = $viewed->ID;
  16.     $post_views = number_format($viewed->views);
  17.     $post_title = esc_attr($viewed->post_title);
  18.     $get_permalink = esc_attr(get_permalink($post_ID));
  19.     $output .= "<li>$beforetitle$post_title$aftertitle";
  20.     if ($show_date) {
  21.       $posted = date(get_option('date_format'), strtotime($viewed->post_date));
  22.       $output .= "$beforedate $posted $afterdate";
  23.     }
  24.     $output .= "$beforecount $post_views $aftercount</li>";
  25.    }
  26.   } else {
  27.    $output = "<li>N/A</li>n";
  28.   }
  29.   echo $output;
  30. }

然后使用下面的函数调用:

  1. <?php get_most_viewed_format(); ?>

瓜皮猪

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: