wordpress添加文章閱讀數量
阿新 • • 發佈:2018-01-14
clas meta _for pos 點擊 global update pda action
將下面代碼添加到functions.php
1 //取得文章的閱讀次數 2 function post_views($before = ‘點擊 ‘, $after = ‘ 次‘, $echo = 1) 3 { 4 global $post; 5 $post_ID = $post->ID; 6 $views = (int)get_post_meta($post_ID, ‘views‘, true); 7 if ($echo) echo $before, number_format($views), $after; 8 else return$views; 9 } 10 function record_visitors() 11 { 12 if (is_singular()) { 13 global $post; 14 $post_ID = $post->ID; 15 if($post_ID) { 16 $post_views = (int)get_post_meta($post_ID, ‘views‘, true); 17 if(!update_post_meta($post_ID, ‘views‘, ($post_views+1))) { 18 add_post_meta($post_ID, ‘views‘, 1, true); 19 } 20 } 21 } 22 } 23 add_action(‘wp_head‘, ‘record_visitors‘);
然後在需要用到的地方加入下面的代碼即可
<?php post_views(); ?>
wordpress添加文章閱讀數量