在 WordPress 开发中,我们需要调用一些网站的统计信息,那么这篇文章将给你一定的帮助。
日志总数:<?php $count_posts = wp_count_posts(); echo $published_posts =$count_posts->publish;?>篇
草稿总数:<?php $count_posts = wp_count_posts(); echo $draft_posts = $count_posts->draft; ?>
页面总数:<?php $count_pages = wp_count_posts('page'); echo $page_posts = $count_pages->publish; ?>
用户总数:<?php $users = $wpdb->get_var("SELECT COUNT(ID) FROM $wpdb->users"); echo $users; ?>
成立时间:<?php echo floor((time()-strtotime("2009-8-23"))/86400); ?> //其中的年月日改成你自己的网站成立时间
分类总数:<?php echo $count_categories = wp_count_terms('category'); ?>个
标签总数:<?php echo $count_tags = wp_count_terms('post_tag'); ?>个
友情链接:<?php $link = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->links WHERE link_visible = 'Y'"); echo $link; ?>个
网站运行:<?php echo floor((time()-strtotime("2011-7-27"))/86400); ?>天
最后更新:<?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; ?>
//WordPress获取本周发布的文章数量
function get_week_post_count(){
$date_query = array(
array(
'after'=>'1 week ago'
)
);$args = array(
'post_type' => 'post',
'post_status'=>'publish',
'date_query' => $date_query,
'no_found_rows' => true,
'suppress_filters' => true,
'fields'=>'ids',
'posts_per_page'=>-1
);
$query = new WP_Query( $args );
return $query->post_count;
}
<?php echo get_week_post_count()?>
//WordPress获取今天更新的文章数量
function get_posts_count_from_today($post_type ='post') {
global $wpdb;
$numposts = $wpdb->get_var(
$wpdb->prepare(
"SELECT COUNT(ID) ".
"FROM {$wpdb->posts} ".
"WHERE post_status='publish' ".
"AND post_type= %s ".
"AND DATE_FORMAT(post_date, '%Y-%m-%d') = %s",
$post_type, date('Y-m-d', time())
)
);
return $numposts;
}
<?php echo get_today_post_count()?>
上面代码为获取默认为“post”这个文章类型,如果你要获取其他文章类型,比如 site,可以这样用:
<?php echo get_posts_count_from_today('site'); ?>
//WordPress获取最近24小时发布的文章数量
function get_posts_count_from_last_24h($post_type ='post') {
global $wpdb;
$numposts = $wpdb->get_var(
$wpdb->prepare(
"SELECT COUNT(ID) ".
"FROM {$wpdb->posts} ".
"WHERE ".
"post_status='publish' ".
"AND post_type= %s ".
"AND post_date> %s",
$post_type, date('Y-m-d H:i:s', strtotime('-24 hours'))
)
);
return $numposts;
}
<?php echo get_posts_count_from_last_24h(); ?>
上面代码为获取默认为“post”这个文章类型,如果你要获取其他文章类型,比如 site,可以这样用:
<?php echo get_posts_count_from_last_24h('site'); ?>