__( '根据设置可以显示不同的文章', 'Bing' ) ) ); $this->default_instance = array( 'title' => __( '文章', 'Bing' ), 'orderby' => 'date', 'descending' => true, 'number' => 5, 'thumbnail' => true, 'date_limit' => 'unlimited', 'exclude_posts' => array(), 'exclude_tax' => array() ); } //小工具内容 function widget( $args, $instance ){ if( empty( $instance ) ) $instance = $this->default_instance; $title = apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base ); echo $args['before_widget']; if( !empty( $title ) ) echo $args['before_title'] . $title . $args['after_title']; $query_args = array( 'ignore_sticky_posts' => true, 'orderby' => $instance['orderby'], 'order' => $instance['descending'] ? 'DESC' : 'ASC', 'posts_per_page' => $instance['number'], 'post__not_in' => $instance['exclude_posts'] ); if( $query_args['orderby'] == 'views' ){ $query_args['meta_key'] = 'views'; $query_args['orderby'] = 'meta_value_num'; } if( $instance['date_limit'] != 'unlimited' ) $query_args['date_query'] = array( 'after' => $instance['date_limit'] ); if( !empty( $instance['exclude_tax'] ) ){ $query_args['tax_query'] = array( 'relation' => 'AND' ); foreach( $instance['exclude_tax'] as $tax => $term ) $query_args['tax_query'][] = array( 'taxonomy' => $tax, 'field' => 'id', 'terms' => $term, 'operator' => 'NOT IN' ); } Bing_sidebar_posts_list( $query_args, $instance['thumbnail'] ); echo $args['after_widget']; } //保存设置选项 function update( $new_instance, $old_instance ){ $instance = array(); //标题 $instance['title'] = strip_tags( $new_instance['title'] ); //文章排序 $all_orderby = array( 'date', 'comment_count', 'views', 'rand' ); $instance['orderby'] = in_array( $new_instance['orderby'], $all_orderby ) ? $new_instance['orderby'] : 'date'; //倒序排列 $instance['descending'] = !empty( $new_instance['descending'] ); //文章数量 $instance['number'] = absint( $new_instance['number'] ); if( $instance['number'] === 0 ) $instance['number'] = 1; //显示缩略图 $instance['thumbnail'] = !empty( $new_instance['thumbnail'] ); //日期限制 $all_date_limit = array( 'unlimited', '1 day ago', '3 day ago', '1 week ago', '1 month ago', '3 month ago', '6 month ago', '1 year ago', '2 year ago', '3 year ago' ); $instance['date_limit'] = in_array( $new_instance['date_limit'], $all_date_limit ) ? $new_instance['date_limit'] : 'unlimited'; //排除文章 $instance['exclude_posts'] = array_map( 'absint', (array) $new_instance['exclude_posts'] ); if( !empty( $instance['exclude_posts'] ) ) $instance['exclude_posts'] = get_posts( array( 'post__in' => $instance['exclude_posts'], 'nopaging' => true, 'post_type' => 'post', 'post_status' => array( 'publish', 'future' ), 'fields' => 'ids', 'update_post_meta_cache' => false, 'update_post_term_cache' => false ) ); //排除分类法 $instance['exclude_tax'] = (array) $new_instance['exclude_tax']; foreach( $instance['exclude_tax'] as $tax_name => $tax ){ if( !taxonomy_exists( $tax_name ) || !is_array( $tax ) || empty( $tax ) ){ unset( $instance['exclude_tax'][$tax_name] ); continue; } $instance['exclude_tax'][$tax_name] = (array) $instance['exclude_tax'][$tax_name]; foreach( $instance['exclude_tax'][$tax_name] as $key => $term_id ){ $instance['exclude_tax'][$tax_name][$key] = absint( $term_id ); if( !term_exists( $instance['exclude_tax'][$tax_name][$key], $tax_name ) ) unset( $instance['exclude_tax'][$tax_name][$key] ); } if( empty( $instance['exclude_tax'][$tax_name] ) ) unset( $instance['exclude_tax'][$tax_name] ); } return $instance; } //设置表单 function form( $instance ){ $instance = wp_parse_args( $instance, $this->default_instance ); $orderby = array( 'date' => __( '发布时间', 'Bing' ), 'comment_count' => __( '评论数量', 'Bing' ), 'views' => __( '浏览次数', 'Bing' ), 'rand' => __( '随机排列', 'Bing' ) ); $date_limit = array( 'unlimited' => __( '无限制', 'Bing' ), '1 day ago' => __( '一天之内', 'Bing' ), '3 day ago' => __( '三天之内', 'Bing' ), '1 week ago' => __( '一周之内', 'Bing' ), '1 month ago' => __( '一个月之内', 'Bing' ), '3 month ago' => __( '三个月之内', 'Bing' ), '6 month ago' => __( '半年之内', 'Bing' ), '1 year ago' => __( '一年之内', 'Bing' ), '2 year ago' => __( '两年之内', 'Bing' ), '3 year ago' => __( '三年之内', 'Bing' ) ); ?>
/>
/>
true ), false ) as $tax_name => $tax ): if ( empty( $tax->labels->name ) || !in_array( 'post', $tax->object_type ) ) continue; $trems = get_terms( $tax_name, 'hide_empty=0' ); if( is_wp_error( $trems ) ) continue; if( empty( $instance['exclude_tax'][$tax_name] ) ) $instance['exclude_tax'][$tax_name] = array(); ?>