WordPress纯代码实现文章浏览次数统计功能

实际上通过插件是很容易解决这个问题的,而且目前市面上大部分主题都自带此功能,但是能用几行代码解决的事情绝不安装插件!此方法是DH参考willin kan大师的my_visitor插件来写的,刷新一次文章页面就统计一次,比较简单实用。

方法一

先在主题文件中找到functions.php文件,在最后面  ?> 的前面加入下面的代码:

function custom_the_views($post_id, $echo=true, $views='views') {
    $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';
    }
    if ($echo)
        echo number_format_i18n($count).$views;
    else
        return number_format_i18n($count).$views;
}
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');

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

<?php if(function_exists('custom_the_views') ) custom_the_views($post->ID); ?>

方法二

还是在主题的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(' ', ' 次'); ?>

调用阅读最多的文章

还是在function.php文件后面添加下方代码:

/// 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;
}

然后在调用地方填写下方代码:

<?php get_most_viewed_format(); ?>
本站提供的所有资源、信息,都来自网络,版权争议与本站无关,所有内容仅限用于学习和研究目的。不得将上述内容用于商业或者非法用途,否则一切后果请用户自负,我们不保证内容的长久性,且通过使用本站内容随之而来的风险与本站无关!
THE END
分享
二维码
< <上一篇
下一篇>>