update post views

This commit is contained in:
spirit 2019-11-22 10:33:36 +08:00
parent ceeeeacf05
commit ae7f444584
1 changed files with 10 additions and 12 deletions

View File

@ -466,17 +466,18 @@ function restyle_text($number) {
}
function set_post_views() {
global $post;
$post_id = intval($post->ID);
$count_key = 'views';
$views = get_post_custom($post_id);
$views = array_key_exists("views",$views) ? intval($views['views'][0]) : 0;
if(is_single() || is_page()) {
if(!update_post_meta($post_id, 'views', ($views + 1))) {
add_post_meta($post_id, 'views', 1, true);
if (is_singular()) {
global $post;
$post_id = intval($post->ID);
if($post_id) {
$views = get_post_meta($post_id, 'views', true);
if(!update_post_meta($post_id, 'views', ($views + 1))) {
add_post_meta($post_id, 'views', 1, true);
}
}
}
}
add_action('get_header', 'set_post_views');
function get_post_views($post_id) {
@ -487,10 +488,7 @@ function get_post_views($post_id) {
return restyle_text(wp_statistics_pages('total','uri',$post_id));
}
} else {
$count_key = 'views';
$views = get_post_custom($post_id);
$views = array_key_exists("views",$views) ? intval($views['views'][0]) : 0;
$post_views = intval(post_custom('views'));
$views = get_post_meta($post_id, 'views', true);
if($views == '') {
return 0;
}else{