获取浏览次数的代码

作者: admin 分类: wordpress主题制作教程 发布时间: 2017-09-12 16:05

同样的方法,把下面的代码放入functions.php文件中,在需要调用函数的地方调用即可:

/**
* getPostViews()函数
* 功能:获取阅读数量
* 在需要显示浏览次数的位置,调用此函数
* @Param object|int $postID 文章的id
* @Return string $count 文章阅读数量
*/
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”;
}
return $count;
}

 

/**
* setPostViews()函数
* 功能:设置或更新阅读数量
* 在内容页(single.php,或page.php )调用此函数
* @Param object|int $postID 文章的id
* @Return string $count 文章阅读数量
*/
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 );
}
}

相关文章

发表评论

邮箱地址不会被公开。 必填项已用*标注

Powered by 草根站长笔记 © 2015-2020 草根站长笔记 Inc.版权所有,禁止转载