December 3, 2014

指定分類目錄下的最新文章列表小工具widget

Tags: wordpresswidget | (3 min read)

根據需求模仿著wordpress官方原生小工具(widget) 寫了一個能夠列出指定分類目錄下最新文章列表的小工具。代碼如下(先貼出來,以後有空再做解釋說明):

function register_catlist_widget(){
register_widget('catlist_widget');
}
add_action('widgets_init','register_catlist_widget');

class Catlist_Widget extends WP_Widget{
/**
 * 定义WP_Widget扩展类,设置小工具名和描述
 * Sets up the widget name and description
 */
public function __construct(){
	$widget_ops = array('classname' => 'catlist_widget', 'description' => '指定分類目錄與顯示數目' );
    // parent::__construct()调用父类定义函数
	parent::__construct(
		'category-post-list', // Base ID
		__('指定分類目錄文章列表','text_domain'), // Name
        $widget_ops // Args
        );

	// add_action( $tag, $function_to_add);
    add_action( 'save_post', array($this, 'flush_widget_cache') );
	add_action( 'deleted_post', array($this, 'flush_widget_cache') );
	add_action( 'switch_theme', array($this, 'flush_widget_cache') );

}

/**
 * Outputs the content of the widget
 * @param  [array] $args     [description]
 * @param  [array] $instance [description]
 * @return [type]           [description]
 */
public function widget($args,$instance){
	$cache = array();
	if ( ! $this->is_preview() ) {
		$cache = wp_cache_get( 'catlist_posts', 'widget' );
	}

	if ( ! is_array( $cache ) ) {
		$cache = array();
	}

	if ( ! isset( $args['widget_id'] ) ) {
		$args['widget_id'] = $this->id;
	}

	if ( isset( $cache[ $args['widget_id'] ] ) ) {
		echo $cache[ $args['widget_id'] ];
		return;
	}

	ob_start();


	$title = ( ! empty( $instance['title'] ) ) ? $instance['title'] : '分類文章列表';
	$title = apply_filters( 'widget_title', $title, $instance, $this->id_base );

	$number = ( ! empty( $instance['num'] ) ) ? absint( $instance['num'] ) : 10;
	if ( ! $number )
		$number = 10;
	$catid = ( ! empty( $instance['cat'] ) ) ? absint( $instance['cat'] ) : 1;
	if ( ! $catid )
		$catid = 1;

	global $post;
	$r = new WP_Query( apply_filters('widget_posts_args', array(
		'posts_per_page'      => $number,
		'cat' 				  => $catid,
		'post_status'         => 'publish',
		'no_found_rows'       => true
		)));
	if ($r->have_posts()) :
?>
	<?php echo $args['before_widget']; ?>
	<?php if ( $title ) {
		echo $args['before_title'] . $title . $args['after_title'];
	} ?>
			<ul>
	<?php while ( $r->have_posts() ) : $r->the_post(); ?>
		<li>
			<a href="<?php the_permalink(); ?>"><?php get_the_title() ? the_title() : the_ID(); ?></a>
		</li>
	<?php endwhile; ?>
	<li><a href="<?php echo get_category_link($catid); ?>">more...</a></li>
	</ul>
	<?php echo $args['after_widget']; ?>
<?php
	// Reset the global $the_post as this query will have stomped on it
	wp_reset_postdata();

	endif;

	if ( ! $this->is_preview() ) {
		$cache[ $args['widget_id'] ] = ob_get_flush();
		wp_cache_set( 'catlist_posts', $cache, 'widget' );
	} else {
		ob_end_flush();
	}
}
public function update( $new_instance, $old_instance ) {
	$instance = $old_instance;
	$instance['title'] = strip_tags($new_instance['title']);
	$instance['num'] = (int) $new_instance['number'];
	$instance['cat'] = (int) $new_instance['cat'];
	$this->flush_widget_cache();

	$alloptions = wp_cache_get( 'alloptions', 'options' );
	if ( isset($alloptions['catlist_widget']) )
		delete_option('catlist_widget');

	return $instance;
}

public function flush_widget_cache() {
	wp_cache_delete('catlist_posts', 'widget');
}


/**
 * Back-end widget form.
 * @see  WP_Widget::form()
 * @param  [array] $instance [description]
 * @return [type]           [description]
 */
public function form($instance){
	$title     = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : '';
	$number    = isset( $instance['num'] ) ? absint( $instance['num'] ) : 10;
	$catid 	   = isset( $instance['cat']) ? absint( $instance['cat'] ):'';
	 ?>
	 <p><label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
	<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; ?>" /></p>

	<p><label for="<?php echo $this->get_field_id( 'number' ); ?>"><?php _e( 'Number of posts to show:' ); ?></label>
	<input id="<?php echo $this->get_field_id( 'number' ); ?>" name="<?php echo $this->get_field_name( 'number' ); ?>" type="text" value="<?php echo $number; ?>" size="3" /></p>
	
	<p><label><?php wp_dropdown_categories(array('name'=>$this->get_field_name('cat'),'selected'=>$instance['cat']));?></label></p>
	<?php
}
}

在小工具(widget)界面添加後的設置效果如下圖示:

参考官方文档:Widgets API