• 首页
  • >
  • wordpress设置并获取浏览次数
  • >
  • 正文

wordpress设置并获取浏览次数

141次阅读

  • getPostViews()函数
  • 功能:获取阅读数量
  • 在需要显示浏览次数的位置,调用此函数
  • @Param object|int $postID 文章的id
  • @Return string $count 文章阅读数量
  • setPostViews()函数
  • 功能:设置或更新阅读数量
  • 在内容页(single.php,或page.php )调用此函数
  • @Param object|int $postID 文章的id
  • @Return string $count 文章阅读数量
<?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 );
	 }
 }




?>

作者:管理员  修订  2019/07/08 15:00:20

WP爱好者专注WordPress在线系统培训,10年来超过十万学员在此获得技术提升和人脉圈子。

WordPress课程已融入到移动搜索、零基础建站、WP爱好者开源主题模板、SEM入门等。

欢迎添加助教微信:tc199361,马上获得优秀学员案例。