博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
WordPress非插件添加文章浏览次数统计功能
阅读量:7042 次
发布时间:2019-06-28

本文共 3741 字,大约阅读时间需要 12 分钟。

一:

转载:http://www.jiangyangangblog.com/26.html

首先在寻找到functions.php.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); ?>

二:

转载:http://www.wpdaxue.com/wordpress-postviews-code.html

在主题的 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;}

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

阅读:

获取浏览次数最多的文章

如果要获取上面的函数统计出来的浏览次数最多的文章,可以在 functions.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 .= "
  • $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
  • "; } } else { $output = "
  • N/A
  • n"; } echo $output;}

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

    你可能感兴趣的文章
    Android Webview打开网页空白
    查看>>
    记一次tortoisegit access denied错误
    查看>>
    前端使用 gulp 解决多项目缓存问题
    查看>>
    Bootstrap基础学习笔记(仅个人学习使用)
    查看>>
    Android项目实战之高仿网易云音乐项目介绍
    查看>>
    头条面试题
    查看>>
    Vue实现一个图片懒加载插件
    查看>>
    Java开发人员必备Linux命令
    查看>>
    记一次中台数据传输同步Elasticsearch失败的车祸现场
    查看>>
    141. Linked List Cycle
    查看>>
    外部资料集合
    查看>>
    如何高效读写百万级的Excel?
    查看>>
    初入门,在vue中使用axios
    查看>>
    "Sed" 高级功能:我这小脑瓜都快绕晕了
    查看>>
    另一个go命令行参数处理器 - cmdr
    查看>>
    WebKit 浏览器内幕之 浏览器特性/网页渲染过程
    查看>>
    一个只需要一行代码即可调出的 Progress,高度定制
    查看>>
    程序人生阶段性随笔
    查看>>
    《锦绣中华》发布会之苏州金鹰国际广场站
    查看>>
    Qtum量子链周报(4月22日-4月28日)
    查看>>