如何解决这段代码每次都会增加 5+ 次 [Total Post View Wordpress]
每次 [Total Post View wordpress] 任何人都必须解决此问题时,此代码会增加 5 倍以上,请帮忙。我正在使用简码方法在单个帖子中打印总视图
function bac_PostViews($post_ID) {
//Set the name of the Posts Custom Field.
$count_key = 'post_views_count';
//Returns values of the custom field with the specified key from the specified post.
$count = get_post_meta($post_ID,$count_key,true);
//If the the Post Custom Field value is empty.
if($count == ''){
$count = 0; // set the counter to zero.
//Delete all custom fields with the specified key from the specified post.
delete_post_Meta($post_ID,$count_key);
//Add a custom (Meta) field (Name/value)to the specified post.
add_post_Meta($post_ID,'0');
return $count . ' View';
//If the the Post Custom Field value is NOT empty.
}else{
$count++; //increment the counter by 1.
//Update the value of an existing Meta key (custom field) for the specified post.
update_post_Meta($post_ID,$count);
//If statement,is just to have the singular form 'View' for the value '1'
if($count == '1'){
return $count . ' View';
}
//In all other cases return (count) Views
else {
return $count . ' Views';
}
}
}
// post total view
function post_total_view() {
if(function_exists('bac_PostViews')) {
echo bac_PostViews(get_the_ID());
}
}
add_shortcode( 'total_post_view','post_total_view' );
检查这张图片-- enter image description here
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。