更新loper侧边栏,添加热门文章Widget

提醒:本文最后更新于 4178 天前,文中所描述的信息可能已发生改变,请仔细核实。

闲着蛋疼,嫌loper东西太少,而且没有更新。最近会尝试自制更新一些函数。

class loper_widget6 extends WP_Widget {
    function loper_widget6() {
        $widget_ops = array('description' => '配合主题样式,多了个悬停箭头出现');
        $this->WP_Widget('loper_widget6', 'loper主题热门文章', $widget_ops);
    }
    function widget($args, $instance) {
        extract($args);
        $title = apply_filters('widget_title',esc_attr($instance['title']));
        $limit = strip_tags($instance['limit']);
        $day = strip_tags($instance['day']);
        echo $before_widget.$before_title.$title.$after_title;
        most_comm_posts($day,$limit);
        echo $after_widget;
    }
    function update($new_instance, $old_instance) {
        if (!isset($new_instance['submit'])) {
            return false;
        }
        $instance = $old_instance;
        $instance['title'] = strip_tags($new_instance['title']);
        $instance['limit'] = strip_tags($new_instance['limit']);
        $instance['day'] = strip_tags($new_instance['day']);
        return $instance;
    }
    function form($instance) {
        global $wpdb;
        $instance = wp_parse_args((array) $instance, array('title'=> '', 'limit' => '', 'day' => ''));
        $title = esc_attr($instance['title']);
        $limit = strip_tags($instance['limit']);
        $day = strip_tags($instance['day']);
?>
        <p>
            <label for="<?php echo $this->get_field_id('title'); ?>">标题:<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" /></label>
        </p>
        <p>
            <label for="<?php echo $this->get_field_id('limit'); ?>">显示数量:<input class="widefat" id="<?php echo $this->get_field_id('limit'); ?>" name="<?php echo $this->get_field_name('limit'); ?>" type="text" value="<?php echo $limit; ?>" /></label>
        </p>
        <p>
            <label for="<?php echo $this->get_field_id('day'); ?>">时间限制:<input class="widefat" id="<?php echo $this->get_field_id('day'); ?>" name="<?php echo $this->get_field_name('day'); ?>" type="text" value="<?php echo $day; ?>" /></label>
        </p>
        <input type="hidden" id="<?php echo $this->get_field_id('submit'); ?>" name="<?php echo $this->get_field_name('submit'); ?>" value="1" />
<?php
    }
}
function most_comm_posts($days=7, $nums=10) {global $wpdb;$today = date("Y-m-d H:i:s"); $daysago = date( "Y-m-d H:i:s", strtotime($today) - ($days * 24 * 60 * 60) );$result = $wpdb->get_results("SELECT comment_count, ID, post_title, post_date FROM $wpdb->posts WHERE post_date BETWEEN '$daysago' AND '$today' ORDER BY comment_count DESC LIMIT 0 , $nums");$output = '';if(empty($result)) {$output = '<li>None data.</li>';} else {foreach ($result as $topten) {$postid = $topten->ID;$title = $topten->post_title;$commentcount = $topten->comment_count;   if ($commentcount != 0) {   $output .= '<ul class="ulstyle"><li><a href="'.get_permalink($postid).'" title="'.$title.'">'.$title.'</a><span class="sidebaraction"></span></li></ul>';}}}echo $output;}
add_action('widgets_init', 'loper_widget6_init');
function loper_widget6_init() {
    register_widget('loper_widget6');
}

转载请注明转自:kn007的个人博客的《更新loper侧边栏,添加热门文章Widget