WordPress 本身是没有运行天数、访客数统计等站点统计功能的,而站点统计却是能够直观展示站点运行状态的好功能,所以就可以自行加入该功能。日常小改主题期间,就自己也忍不住研究了下,即使轩陌大大已经弄了些,但自己还是想在加点什么,那么下面我就将在百度上找到的一些经常使用到的函数罗列出来,方便自己改时参考,也奉献给大家看看(也许还有其他的函数,欢迎留言评论留下)!
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("2016-08-18"))/86400); ?>
记得将上述日期修改为你的建站日期
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 $users = $wpdb->get_var("SELECT COUNT(ID) FROM $wpdb->users"); echo $users; ?>
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; ?>
11、访问总数:
<?php
$counterFile = "counter.txt";
$counterBackupFile = "counter_bak.txt";
function displayCounter($counterFile, $counterBackupFile) {
$fp = fopen($counterFile, "r");
$num = fgets($fp, 10);
fclose($fp);
$fp = fopen($counterBackupFile, "r");
$numBak = fgets($fp, 10);
fclose($fp);
if ($num < 10) {
if ($numBak > 10) {
$num = $numBak;
}
}
if (!is_user_logged_in()) {
$num += 1;
$fp = fopen($counterFile, "w");
fputs($fp, $num, 10);
fclose($fp);
if ($num % 20 == 0 && $num > 10) {
$fp = fopen($counterBackupFile, "w");
fputs($fp, $num, 10);
fclose($fp);
}
}
echo "$num"." 人次";
}
if (!file_exists($counterFile)) {
fopen($counterFile, "w");
fputs($fp, 0, 10);
fclose($fp);
}
if (!file_exists($counterBackupFile)) {
fopen($counterBackupFile, "w");
fputs($fp, 0, 10);
fclose($fp);
}
displayCounter($counterFile, $counterBackupFile);
?>
最后
简单的介绍下食用方法
参考上面的函数,找到正在使用的主题的文件夹内的 sidebar.php 打开,创建一个 div ,用li来包裹函数,如果加运行天数的话,那里面的日期改为你的建站日期即可。然后保存。如果你嫌样式单调了,就加点css美化下,大功告成!(代码和方法都给了,再不会用的话....)
<div>
<h1>站点统计</h1>
<ul>
<li>文章总数:<?php $count_posts = wp_count_posts(); echo $published_posts = $count_posts->publish;?> 篇</li>
<li>页面总数:<?php echo wp_count_posts('page')->publish;?> 个</li>
<li>评论总数:<?php echo wp_count_comments()->total_comments?> 条</li>
<li>分类总数:<?php echo wp_count_terms('category')?> 个</li>
<li>标签总数:<?php echo wp_count_terms('post_tag')?> 个</li>
<li>运行天数:<?php echo floor((time()-strtotime("2014-04-05"))/86400);?> 天</li>
</ul>
</div>
<!--函数也可以这样写-->
<div>
<h1>站点统计</h1>
<ul>
<li>文章总数:<?php $count_posts = wp_count_posts(); echo $published_posts = $count_posts->publish . '篇'; ?></li>
<li>页面总数:<?php echo wp_count_posts('page')->publish . '个';?></li>
<li>评论总数:<?php echo wp_count_comments()->total_comments . '条';?></li>
<li>分类总数:<?php echo wp_count_terms('category') . '个';?></li>
<li>标签总数:<?php echo wp_count_terms('post_tag') . '个';?></li>
<li>运行天数:<?php echo floor((time()-strtotime("2014-04-05"))/86400) . '天';?></li>
</ul>
</div>