如何实现网站的基本信息统计功能,包括网站文章数、评论数、建站天数等等。
上代码
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 echo $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->comments");?>
4、建站天数:
<?php echo floor((time()-strtotime("2011-11-12"))/86400); ?>
记得修改 2011-11-12 为你的建站日期
5、标签总数:
<?php echo $count_tags = wp_count_terms('post_tag'); ?>
6、页面总数:
<?php $count_pages = wp_count_posts('page'); echo $page_posts = $count_pages->publish; ?>
7、分类总数:
<?php echo $count_categories = wp_count_terms('category'); ?>
8、链接总数:
<?php $link = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->links WHERE link_visible = 'Y'"); echo $link; ?>
9、最后更新:
<?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; ?>
10、总浏览量:
首先,在主题的functions.php文件中加入一下代码:
//WordPress获取站点总浏览量
function all_view() /*注意这个函数名,调用的就是用它了*/
{
global $wpdb;
$count=0;
$views= $wpdb->get_results("SELECT * FROM $wpdb->postmeta WHERE meta_key='views'");
foreach($views as $key=>$value)
{
$meta_value=$value->meta_value;
if($meta_value!=' ')
{
$count+=(int)$meta_value;}
}
return $count;}
之后,在需要显示总浏览量的地方插入一下代码:
<?php echo all_view(); ?>