少龙代理™'s Archivers

From C.K. on 2015-11-20 07:35:30

wordpress获取文章浏览次数及本站浏览总数

1、在你的function.php文件中,扔如下面一坨代码:

[cc lang="php"]/*总浏览数*/
function lo_all_view(){
global $wpdb;
$count=0;
$views=$wpdb->get_results("SELECT * FROM $wpdb->postmeta WHERE meta_key='views'");
foreach($views as $key=>$value)
{
$meta_value=$value->meta_value;
if($meta_value!=' ')
{
$count+=(int)$meta_value;
}
}
return $count;
}

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);
}
}
}
[/cc]

2、调用代码:

文章浏览次数的调用代码为:文章浏览次数ID);

[cc lang="php"][/cc]

本站总计浏览次数的调用代码为:总浏览量:

[cc lang="php"][/cc]

3、现在你可以看到你的博客已经开始统计浏览次数了

查看完整版本: wordpress获取文章浏览次数及本站浏览总数

Tags: