來源于:
https://www.wpdaxue.com/wordpress-postviews-code.html
如下:
1.在主題的 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');
?
/// 函數名稱:post_views
/// 函數作用:取得文章的閱讀次數
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;
}
2.在需要顯示該統計次數的地方使用下面的代碼調用:
<?php閱讀: post_views(' ', ' 次'); ?>
參考:
http://www.zixuephp.com/wzht/wordpress/20141128_8004.html