为WordPress添加百度访客统计

0x00.在function.php中添加功能

//获取post_views
function get_post_views ($post_id)
{
 $count_key = 'views';
 $count = get_post_meta($post_id, $count_key, true);
 if ($count == '') 
  {
   delete_post_meta($post_id, $count_key);
   add_post_meta($post_id, $count_key, '0');
   $count = '0';
  }
 echo number_format_i18n($count);
}
//设置post_views
function set_post_views () 
{
 global $post;
 $post_id = $post -> ID;
 $count_key = 'views';
 $count = get_post_meta($post_id, $count_key, true);
 if (is_single() || is_page()) 
  {
   if ($count == '') {
   delete_post_meta($post_id, $count_key);
   add_post_meta($post_id, $count_key, '0');
  } else 
  {
   update_post_meta($post_id, $count_key, $count + 1);
  }
}
}
add_action('get_header', 'set_post_views');

0x01.echo

echo get_post_views(get_the_ID());

即可输出访问次数。