空の領域

又一个坑爹的神站 大人說:要清爽 要低調

0%

免插件实现postviews统计

又是一篇讲代码的……唉最近都干了些什么,怎么说都说不完。

上次给姗宝的一段代码据说刷新就会增加1计数,一点都不好用嘛,所以这次换了一个代码(来自倡萌)

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

然后在需要显示的地方加入:

被围观

然后的然后……倡萌还增加了两个函数:

1、得到最多阅读的文章(五枚入):

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

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

    反正俺们一个都用不到= =、


    accordium请在index.php里找到

    修改为红色加粗的那一步的代码即可。(人家的文章计数器因此又被清零了一次QAQ)

    于是 插件到代码 代码到代码的转换都会清空计数器,请视你的主题更换频率以及你对该数据的重视度而定是否使用此条代码。

    欢迎关注我的其它发布渠道