将下面代码添加到Functions.php
//取得文章的阅读次数
function post_views($before = '点击 ', $after = ' 次', $echo = 1)
{
global $post;
$post_ID = $post->ID;
$views = (int)get_post_meta($post_ID, 'views', true);
if ($echo) echo $before, number_format($views), $after;
else return $views;
}
function record_visitors()
{
if (is_singular()) {
global $post;
$post_ID = $post->ID;
if($post_ID) {
$post_views = (int)get_post_meta($post_ID, 'views', true);
if(!update_post_meta($post_ID, 'views', ($post_views+1))) {
add_post_meta($post_ID, 'views', 1, true);
}
}
}
}
add_action('wp_head', 'record_visitors');
在想调用的地方,直接在while ( have_posts() ) {内使用下面代码即可
<?php post_views(); ?>
原文地址: http://www.xz-src.com/1044.html
本文介绍了一种在WordPress中实现文章阅读次数统计的方法。通过在functions.php文件中添加特定PHP代码,可以记录每篇文章的访问量,并在文章页面显示阅读次数。
501

被折叠的 条评论
为什么被折叠?



