WordPress教程

WordPress 开发各种统计信息获取一览表

阿里云

在 WordPress 开发中,我们需要调用一些网站的统计信息,那么这篇文章将给你一定的帮助。

section

  1.     日志总数:<?php $count_posts = wp_count_posts(); echo $published_posts =$count_posts->publish;?>篇
  2.     草稿总数:<?php $count_posts = wp_count_posts(); echo $draft_posts = $count_posts->draft; ?>
  3.     页面总数:<?php $count_pages = wp_count_posts('page'); echo $page_posts = $count_pages->publish; ?>
  4.     用户总数:<?php $users = $wpdb->get_var("SELECT COUNT(ID) FROM $wpdb->users"); echo $users; ?>
  5.     成立时间:<?php echo floor((time()-strtotime("2009-8-23"))/86400); ?> //其中的年月日改成你自己的网站成立时间
  6.     分类总数:<?php echo $count_categories = wp_count_terms('category'); ?>个
  7.     标签总数:<?php echo $count_tags = wp_count_terms('post_tag'); ?>个
  8.     友情链接:<?php $link = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->links WHERE link_visible = 'Y'"); echo $link; ?>个
  9.     网站运行:<?php echo floor((time()-strtotime("2011-7-27"))/86400); ?>天
  10.     最后更新:<?php $last = $wpdb->get_results("SELECT MAX(post_modified) AS MAX_m FROM $wpdb->posts WHERE (post_type = 'post' OR post_type = 'page') AND (post_status = 'publish' OR post_status = 'private')");$last = date('Y年n月j日', strtotime($last[0]->MAX_m));echo $last; ?>
也想出现在这里?联系我们
创客主机

section

  1.     //WordPress获取本周发布的文章数量
  2.     function get_week_post_count(){
  3.     $date_query = array(
  4.     array(
  5.     'after'=>'1 week ago'
  6.     )
  7.     );$args = array(
  8.     'post_type' => 'post',
  9.     'post_status'=>'publish',
  10.     'date_query' => $date_query,
  11.     'no_found_rows' => true,
  12.     'suppress_filters' => true,
  13.     'fields'=>'ids',
  14.     'posts_per_page'=>-1
  15.     );
  16.     $query = new WP_Query( $args );
  17.     return $query->post_count;
  18.     }
  1.     <?php echo get_week_post_count()?>
  1.     //WordPress获取今天更新的文章数量
  2.     function get_posts_count_from_today($post_type ='post') {
  3.     global $wpdb;
  4.     $numposts = $wpdb->get_var(
  5.     $wpdb->prepare(
  6.     "SELECT COUNT(ID) ".
  7.     "FROM {$wpdb->posts} ".
  8.     "WHERE post_status='publish' ".
  9.     "AND post_type= %s ".
  10.     "AND DATE_FORMAT(post_date, '%Y-%m-%d') = %s",
  11.     $post_type, date('Y-m-d', time())
  12.     )
  13.     );
  14.     return $numposts;
  15.     }
  1.     <?php echo get_today_post_count()?>

上面代码为获取默认为“post”这个文章类型,如果你要获取其他文章类型,比如 site,可以这样用:

  1.     <?php echo get_posts_count_from_today('site'); ?>
  1.     //WordPress获取最近24小时发布的文章数量
  2.     function get_posts_count_from_last_24h($post_type ='post') {
  3.     global $wpdb;
  4.     $numposts = $wpdb->get_var(
  5.     $wpdb->prepare(
  6.     "SELECT COUNT(ID) ".
  7.     "FROM {$wpdb->posts} ".
  8.     "WHERE ".
  9.     "post_status='publish' ".
  10.     "AND post_type= %s ".
  11.     "AND post_date> %s",
  12.     $post_type, date('Y-m-d H:i:s', strtotime('-24 hours'))
  13.     )
  14.     );
  15.     return $numposts;
  16.     }
  1.     <?php echo get_posts_count_from_last_24h(); ?>

上面代码为获取默认为“post”这个文章类型,如果你要获取其他文章类型,比如 site,可以这样用:

  1.     <?php echo get_posts_count_from_last_24h('site'); ?>

WordPress 开发各种统计信息获取一览表

已有 194 人购买
查看演示升级 VIP立刻购买

收藏
(0)

发表回复

热销模板

Ashade - 作品展示摄影相册WordPress汉化主题
LensNews

本站承接 WordPress / PbootCMS / DedeCMS 等
系统建站、仿站、开发、定制等业务!