阅读量能显示访客浏览了你的博客文章的次数。但有些wordpress主题没有显示阅读量数据,如果想加上,可以复制如下代码,粘贴到function.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');
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;
}
然后,在content.php和singe.php文件的适当位置,分别加入以下相同代码:
<?php post_views('','次');?>
现在,在wordpress的前台页面就能看到每篇文章的阅读量了。
如果你还想在wordpress后台的文章列表中显示每篇阅读量,可以在function.php文件中继续加入以下代码:
//在后台文章列表增加一列数据
add_filter( 'manage_posts_columns', 'customer_posts_columns' );
function customer_posts_columns( $columns ) {
$columns['views'] = '浏览次数';
return $columns;
}
//输出浏览次数
add_action('manage_posts_custom_column', 'customer_columns_value', 10, 2);
function customer_columns_value($column, $post_id){
if($column=='views'){
$count = get_post_meta($post_id, 'views', true);
if(!$count){
$count = 0;
}
echo $count;
}
return;
}
如果想根据阅读量对文章进行排序,则可加入以下代码:
1 $hot_args = array(
2 'cat' => $hot_category_id, // 使用分类ID
3 'posts_per_page' => 6, // 获取所有文章
4 'ignore_sticky_posts' => 1, // 忽略置顶文章
5 'has_post_thumbnail' => true, // 只获取带有特色图片的文章
6 'meta_key' => 'views',
7 'orderby' => 'meta_value',
8
9 'order' => 'DESC' // 倒序排列
10 );
以上代码,转载参考自郑州谷多软件的博客。