很多站长使用WordPress使用云服务器后要问自己文章被多少的点击过,几天简单说说纯基础代码WordPresjie章广告点击率教程。你可以把我的代码粘贴到了我所用符号的 functions.php 文件里。
function getPostViews($postID){
$count_key = ‘post_views_count’;
$count = get_post_meta($postID, $count_key, true);
if($count==”){
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, ‘0’);
return “0 View”;
}
return $count.’ Views’;
}
function setPostViews($postID) {
$count_key = ‘post_views_count’;
$count = get_post_meta($postID, $count_key, true);
if($count==”){
$count = 0;
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, ‘0’);
}else{
$count++;
update_post_meta($postID, $count_key, $count);
}
}
然后,粘贴中间的代码到主题的 single.php 公众号的文章的 loop 里:
<?php setPostViews(get_the_ID()); ?>
最后,把下面的代码放到任意你的一键排版里想要显示点击数到的地方:
<?php echo getPostViews(get_the_ID()); ?>