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() { function set_post_views() {
if (is_singular()) {
global $post; global $post;
$post_id = intval($post->ID); $post_id = intval($post->ID);
$count_key = 'views'; if($post_id) {
$views = get_post_custom($post_id); $views = get_post_meta($post_id, 'views', true);
$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))) { if(!update_post_meta($post_id, 'views', ($views + 1))) {
add_post_meta($post_id, 'views', 1, true); add_post_meta($post_id, 'views', 1, true);
} }
} }
} }
}
add_action('get_header', 'set_post_views'); add_action('get_header', 'set_post_views');
function get_post_views($post_id) { 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)); return restyle_text(wp_statistics_pages('total','uri',$post_id));
} }
} else { } else {
$count_key = 'views'; $views = get_post_meta($post_id, 'views', true);
$views = get_post_custom($post_id);
$views = array_key_exists("views",$views) ? intval($views['views'][0]) : 0;
$post_views = intval(post_custom('views'));
if($views == '') { if($views == '') {
return 0; return 0;
}else{ }else{