10973

关于WordPress的博客统计功能,比如本博客首页的博客统计功能。这个主题不是我写的。主题是由“热血洒红尘”创作的免费主题,由“啸月天狼”进行二次修改。

博客统计功能的图片

该统计功能在主题中是注册到小工具的如果你还想再其他地方使用自行修改。代码如下


/***************************
* 网站统计
**************************/
class widget_tongji extends WP_Widget {
function widget_tongji() {
global $theme_name;
$option = array('classname' => 'widget_tongji', 'description' => '网站统计('.$theme_name.')' );
$this->WP_Widget(false, '网站统计('.$theme_name.')', $option);
}
function widget($args, $instance) {
extract($args, EXTR_SKIP);
echo $before_widget;
$title = empty($instance['title']) ? '最新评论' : apply_filters('widget_title', $instance['title']);
$time = empty($instance['time']) ? '建站日期' : apply_filters('widget_count', $instance['time']);

echo $before_title . $title . $after_title;
echo '<ul class="tongji">';?>
<li>文章总数:<?php $count_posts = wp_count_posts();echo $published_posts = $count_posts->publish;?>篇</li>
<li>评论总数:<?php $count_comments = get_comment_count();echo $count_comments['approved'];?>条</li>
<li>页面总数:<?php $count_pages = wp_count_posts('page'); echo $page_posts = $count_pages->publish; ?> 个</li>
<li>分类总数:<?php echo $count_categories = wp_count_terms('category'); ?>个</li>
<li>标签总数:<?php echo $count_tags = wp_count_terms('post_tag'); ?>个</li>
<li>运行天数:<?php echo floor((time()-strtotime($time))/86400); ?> 天</li>
<?php
echo '</ul>';
echo $after_widget;
}
function update($new_instance, $old_instance) {
$instance = $old_instance;
$instance['title'] = strip_tags($new_instance['title']);
$instance['time'] = strip_tags($new_instance['time']);
return $instance;
}
function form($instance) {
$instance = wp_parse_args( (array) $instance, array( 'title' => '', 'count' => '' ) );
$title = strip_tags($instance['title']);
$time = strip_tags($instance['time']);

echo '<p><label>标题:<input id="'.$this->get_field_id('title').'" name="'.$this->get_field_name('title').'" type="text" value="'.attribute_escape($title).'" size="24" /></label></p>';
echo '<p><label>建站日期:<input id="'.$this->get_field_id('time').'" name="'.$this->get_field_name('time').'" type="text" value="'.attribute_escape($time).'" size="24" /></label></p>';
}
}
add_action('widgets_init', create_function('', 'return register_widget("widget_tongji");'));
?>

转载原创文章请注明,转载自: 罗正荣个人博客 » 博客统计功能

4条评论

Loading...

发表评论

电子邮件地址不会被公开。 必填项已用*标注

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  1. Everytime Britney Spears 3:53