马上注册,结交更多好友
您需要 登录 才可以下载或查看,没有账号?立即注册
×
无插件添加博客统计方法:一、安装插件 Enhanced Text Widget ,它能让你在侧边栏小工具中直接使用 PHP代码。 二、进入外观--小工具--添加一个名为“Enhanced Text”的小工具,标题为“博客统计”,内容为以下代码: - <?php
- /**
- * 模块名称:侧边栏小工具 - 博客统计
- * 模块描述:在侧边栏增加一个小工具,显示博客的统计信息
- ?>
- <?php
- $establish_time = '2012-05-11'; // 设置博客的成立时间,格式如 2012-05-11
- ?>
- <?php
- global $wpdb;
- // 文章总数
- $count_posts = wp_count_posts();
- $ebs_posts = $count_posts->publish;
- // 评论总数
- $count_comments = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->comments");
- $ebs_comments = $count_comments;
- // 成立时间
- $ebs_establish = sprintf("%d", substr($establish_time, 0, 4)) . ' 年 ' . sprintf("%d", substr($establish_time, 5, 2)) . ' 月 ' . sprintf("%d", substr($establish_time, 8, 2)) . ' 日';
- // 运行时间
- $run_time = floor((time() - strtotime($establish_time)) / 86400); // 博客已经运行了 $run_time 天
- $ebsr_year = floor($run_time / 365); // 博客已经运行了 $ebsr_year 年
- $ebsr_month = ceil(($run_time % 365) / 30); // 博客已经运行了 $ebsr_month 月
- $ebsr_day = ($run_time % 365) % 30; // 博客已经运行了 $ebsr_day 日
- $ebs_runtime = $ebsr_year . ' 年 ' . $ebsr_month . ' 月 ' . $ebsr_day . ' 日';
- // 最后更新
- $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')");
- $ebs_last = date('Y 年 n 月 j 日', strtotime($last[0]->MAX_m));
- ?>
- <?php
- $output = '';
- $output .= '<ul id="efanyh_blogstats">';
- $output .= '<li><span>文章数量: ' . $ebs_posts . ' 篇</span></li>';
- $output .= '<li><span>评论数量: ' . $ebs_comments . ' 条</span></li>';
- $output .= '<li><span>成立时间: ' . $ebs_establish . '</span></li>';
- $output .= '<li><span>运行天数: ' . $ebs_runtime . '</span></li>';
- $output .= '<li><span>最后更新: ' . $ebs_last . '</span></li>';
- $output .= '</ul>';
- echo $output;
- ?>
复制代码三、保存即可在前台看到效果,样式运用在不同主题上可能需要微调以达到最佳的显示效果。
|