首页 > CMS教程 > WordPress    日期:2018-09-01 / 来自互联网 / 浏览

将下面代码添加到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(); ?>

觉得上面的内容有用吗?快来点个赞吧!

点赞() 我要打赏

温馨提示 : 本站内容来自会员投稿以及互联网,所有源码及教程均为作者总结编辑,请大家在使用过程中提前做好备份,以免发生无法预知的错误,源码类教程请勿直接用于生产环境!

 可能感兴趣的文章