wordpress设置统计文章浏览次数的代码

南国老符 南国老符 最后更新于:2021-08-24 浏览:1316

wordpress设置统计文章浏览次数,把如下代码加到模板所在目录的functions.php里即可:


/**
 * 统计文章浏览次数
 * 调用:get_post_views($post->ID);
 * @author albert
 * @date 2021.07.05
 */
function get_post_views ($post_id) {
	$count_key = 'views';
	$count = get_post_meta($post_id, $count_key, true);

	if ($count == '') {
		delete_post_meta($post_id, $count_key);
		add_post_meta($post_id, $count_key, '0');
		$count = '0';
	}

	echo number_format_i18n($count);
}

function set_post_views () {
	global $post;

	$post_id = $post -> ID;
	$count_key = 'views';
	$count = get_post_meta($post_id, $count_key, true);

	if (is_single() || is_page()) {
		if ($count == '') {
			delete_post_meta($post_id, $count_key);
			add_post_meta($post_id, $count_key, '0');
		} else {
			update_post_meta($post_id, $count_key, $count + 1);
		}
	}
}
add_action('get_header', 'set_post_views');



点赞 (0)

收藏 (0)
转载请务必注明: 来源:《建站管家》https://www.df81.com/news/218.html

本站部分信息来源网络,如有侵权,请联系QQ:1062129401删除。

上一篇: wordpress自定义设置SEO标题、关键词和描述

下一篇: wordpress给分类添加:前后台SEO标题、关键词和描述的功能代码

请先登录后,再发表评论 ~ ~
发表评论
评论 (0)

发表帖子
广告位招租( ¥5 / 天 )
点击咨询
最新帖子

南国老符
2025-05-26 16:54
南国老符
2024-09-29 10:29
南国老符
2023-12-15 21:41
广告位招租( ¥5 / 天 )
点击咨询
最近评论

U
user1379
回复:版本号多少?
U
user1379
回复:新版本修复了,下载新版本即可
U
user1379
U
user1304
回复:默认没有这个功能,你可以自行二次开发后期可能会考虑在站群版本上加入此功能及商城功能
Y
yuanis90
回复:后台->语言->全局管理->模型管理->编辑->启用关键词内链即可
Y
yuanis90
回复:后台已经启用了,但是前台产品说明的关分键词还是没有自动链接到,
购买VIP
购买授权
余额充值
发表帖子
客服微信