步骤一
创建WordpressRunningInfoStat.php 文件(文件名任意)
<?php
// 定义小工具的类 WordPressRunningInfoStat
class WordPressRunningInfoStat extends WP_Widget{
function WordPressRunningInfoStat(){
// 定义小工具的构造函数
$widget_ops = array('classname' => 'widget_blogstat', 'description' => '显示博客的统计信息');
$this->WP_Widget(false, '博客统计', $widget_ops);
}
function form($instance){
// 表单函数,控制后台显示
// $instance 为之前保存过的数据
// 如果之前没有数据的话,设置默认量
$instance = wp_parse_args(
(array)$instance,
array(
'title' => '博客统计',
'establish_time' => '2016-09-28'
)
);
$title = htmlspecialchars($instance['title']);
$establish_time = htmlspecialchars($instance['establish_time']);
// establish_time => 建站日期
// 表格布局输出表单
$output = '<table>';
$output .= '<tr><td>标题</td><td>';
$output .= '<input id="'.$this->get_field_id('title') .'" name="'.$this->get_field_name('title').'" type="text" value="'.$instance['title'].'" />';
$output .= '</td></tr><tr><td>建站日期:</td><td>';
$output .= '<input id="'.$this->get_field_id('establish_time') .'" name="'.$this->get_field_name('establish_time').'" type="text" value="'.$instance['establish_time'].'" />';
$output .= '</td></tr></table>';
echo $output;
}
function update($new_instance, $old_instance){
// 更新数据的函数
$instance = $old_instance;
// 数据处理
$instance['title'] = strip_tags(stripslashes($new_instance['title']));
$instance['establish_time'] = strip_tags(stripslashes($new_instance['establish_time']));
return $instance;
}
function widget($args, $instance){
extract($args); //展开数组
$title = apply_filters('widget_title',empty($instance['title']) ? ' ' : $instance['title']);
$establish_time = empty($instance['establish_time']) ? '2013-01-27' : $instance['establish_time'];
echo $before_widget;
echo $before_title . $title . $after_title;
echo '<div style="margin-top: 15px;"><section class="widget widget_categories wrapper-md clear"><ul class="list-group">';
$this->efan_get_blogstat($establish_time);
echo '</ul></section></div>';
echo $after_widget;
}
function efan_get_blogstat($establish_time /*, $instance */){
// 相关数据的获取
global $wpdb;
$count_posts = wp_count_posts();
$published_posts = $count_posts->publish;
$draft_posts = $count_posts->draft;
$comments_count = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->comments");
$time = floor((time()-strtotime($establish_time))/86400);
$count_tags = wp_count_terms('post_tag');
$count_pages = wp_count_posts('page');
$page_posts = $count_pages->publish;
$count_categories = wp_count_terms('category');
$link = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->links WHERE link_visible = 'Y'");
$users = $wpdb->get_var("SELECT COUNT(ID) FROM $wpdb->users");
$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-m-d', strtotime($last[0]->MAX_m));
$total_views = $wpdb->get_var("SELECT SUM(meta_value+0) FROM $wpdb->postmeta WHERE meta_key = 'views'");
$uptime = trim(file_get_contents('/proc/uptime'));
$uptime = explode(' ', $uptime);
$uptime = $uptime[0];
$uptime = round($uptime / 86400, 2);
$this->statItem('fas fa-file-alt', '文章数量', $published_posts);
$this->statItem('fas fa-comments', '评论数量', $comments_count);
$this->statItem('fas fa-tags', '标签数量', $count_tags);
$this->statItem('fas fa-blog', '建站日期', $establish_time);
$this->statItem('far fa-clock', '累计安全运行', $time . ' 天');
$this->statItem('fas fa-power-off', '自上次服务重启后持续运行', $uptime . ' 天');
$this->statItem('fas fa-sync-alt', '最后更新', $last);
}
function statItem($fa, $desc, $data) {
echo '<li class="list-group-item">';
echo '<i class="' . $fa . '"></i>';
echo '<span class="badge pull-right">' . $data . '</span>';
echo ' ' . $desc. ' ';
echo '</li>';
}
}
function WordPressRunningInfoStat(){
// 注册小工具
register_widget('WordpressRunningInfoStat');
}
add_action('widgets_init','WordpressRunningInfoStat');
?>
步骤二
将新建的 WordPressRunningInfoStat.php 文件 放到主题的根目录下
步骤三
在主题里的 function.php 函数里 ?>前 添加 include(“WordPressRunningInfoStat.php”); (注意文件名写自己命名的)
include("WordpressRunningInfoStat.php");
步骤四
到“后台——外观——小工具”中添加“博客统计”小工具即可。
预览
我觉得太丑了,修改了css,这个css代码放在主题自定义css里就行,具体参数按自己喜欢的改。
/*给博客统计加阴影、四角变圆滑、背景变透明*/
#wordpressrunninginfostat-2 section {
box-shadow:0 10px 10px 5px #bbbdc1;
border-radius:22px;
}
.list-group-item:first-child{
border-top-left-radius: 20px;
border-top-right-radius: 20px;
}
.list-group-item:last-child{
border-bottom-left-radius: 20px;
border-bottom-right-radius: 20px;
}
.list-group-item{
background-color:transparent;
}
修改后的样式,见本文右侧【文章推荐】小工具,最后一项
必须 注册 为本站用户, 登录 后才可以发表评论!