也想出现在这里?联系我们

代码实现wordpress浏览统计功能,省去使用WP-PostViews插件!

  • 文章介绍
  • 升级版本
  • 评价&建议

以前小编还是小白的时候,玩wordpress主题的时候经常喜欢安装一些插件来增加wordpress的功能性,比如说wordpress浏览统计功能,比如说WP-PostViews插件。。。之后发现,插件安装的越多,wordpress速度越来越慢,之后经过度娘的查询后发现,安装过多的插件对wordpress主题速度影响非常大,因为会产生很多的查询,造成了数据变慢,而最近小编的网站正在渐渐的代码化了,把能够代码完成尽可能的去代码完成,wordpress瘦身,从wordpress插件的减少使用开始!

那么今天小编要讲的就是通过代码实现wordpress浏览统计功能,就不谈使用别的主题,就小编正在使用的知更鸟主题来说,就有一个必须要安装的插件WP-PostViews,而且还不能升级,小编每次看到wordpress的升级按钮就很恼火。今天闲来无事,捣鼓了一番,整理出来了解决方案,下面说重点了!

基本我们使用WP-PostViews插件都是想有统计和浏览数的功能,那么下面的一段代码就是来实现这两个功能的!

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

获取浏览次数最多的文章
如果要获取上面的函数统计出来的浏览次数最多的文章,可以在 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 .= "<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(); ?>

怎么样,很简单吧。。。有什么疑问本帖回复解答!

有用6
  • 2013.06.22初次和大家见面了!

等待您对该主题的建议

发表评论

还能输入240个字

Hi, 欢迎加入Wordpress技术交流群,带你装逼带你飞!

我要入群
也想出现在这里?联系我们
wordpress加速

我来推荐一个更牛逼的给你看看?

  • 猛戳我吧