wordpress文章浏览量代码,老生常谈,不再多说。
将下面代码加入到functiuons.php中
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
/*实现文章浏览次数*/ 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文件中要显示浏览次数的位置
1 2 |
<?php setPostViews(get_the_ID()); ?> <?php echo getPostViews(get_the_ID()); ?> |
需要在哪个页面显示文章的浏览次数,就在相应的模板文件中加入
1 |
<?php echo getPostViews(get_the_ID()); ?> |