用wordpress 搭建的博客或许企业站都有着一样的需求,就是在内容页面须要统计这个页面的阅读次数,以轻易站长检察某个页面的仿题目,平常对代码不熟悉的朋侪都邑用到阅读次数统计插件(WP-PostViews),然则尽人皆知,一个站点假如插件装置的多了会对SEO优化 异常不利,那末下面给人人分享一个应用代码来完成这类阅读次数的统计功用。
1、在functions.php函数文件的未尾另起一行,增加以下代码。
<?php /* Postviews start */ 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; } 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); } } /* Postviews start end*/ ?>
2、在您须要显现统计阅读次数的页面,例如在single.php内容页面中的<?php endwhile; ?>和<?php endif; wp_reset_query(); ?>轮回语句的中心增加以下代码:
<?php setPostViews(get_the_ID());?>
增加后的显现,如:
<?php endwhile; ?> <?php setPostViews(get_the_ID());?> <?php endif; wp_reset_query(); ?>
3、末了在您须要显现统计的处所增加以下代码:
<?php echo getPostViews(get_the_ID()); ?> 次阅读
更多wordpress相干技术文章,请接见wordpress教程栏目举行进修!
以上就是wordpress查询阅读量须要用什么代码的细致内容,更多请关注ki4网别的相干文章!