当前位置:首页>网络教程>纯代码实现WordPress文章点击数教程

纯代码实现WordPress文章点击数教程

很多站长使用WordPress搭建网站后想要知道自己文章被多少人点击过,几天分享一下纯代码实现WordPress文章点击数教程。你可以 ...

很多站长使用WordPress搭建网站后想要知道自己文章被多少人点击过,几天分享一下纯代码实现WordPress文章点击数教程。你可以把下面的代码粘贴到你所用主题的 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()); ?>

给TA打赏
共{{data.count}}人
人已打赏
网络教程

WordPress设置访问权限教程

2022-7-15 17:10:09

网络教程

WordPress 5.7 引入函数来检查文章是否可以公开查看

2022-7-15 17:10:28

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索