用户点击浏览博客中的插件也使用Postviews,然而,鉴于尽可能少地使用插件的原则,我们可以在不使用插件的情况下使用脚本。在这里,整理这个脚本可以替换我们的浏览插件。添加方法非常简单。让我们看看下面的操作。
首先,把脚本放在当前的主题上functions.php中
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’);
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;
}
保存脚本。
二、使用脚本
可将调用脚本放在需要显示浏览次数的位置,实现无插件浏览次数的调用。