ウィジェット クラス サンプル

3.01
カスタムポスト対応

 

/**
  * categoryWidget Class
  */
 add_action('widgets_init', create_function('', 'return register_widget("tmn_categoryWidget");'));
 
 
 class tmn_categoryWidget extends WP_Widget {
 
 
  /** constructor */
  function tmn_categoryWidget() {
  
 	$widget_ops= array( 'description'=> '表示するカテゴリをひとつ指定して、属するエントリの一覧を日付 タイトルリンクで表示します。') ;
 
  	parent::WP_Widget(false, 'カテゴリウィジェット',$widget_ops);
 	
 	
 	wp_reset_query();
  }
  
 
  /** @see WP_Widget::widget */
  function widget($args, $instance) {
  	global $post;
  	extract( $args );
  	echo $before_widget; 
  	echo $before_title. wp_specialchars($instance['title']). $after_title;
 
 	$names 	= get_post_types( array( 'public' => true, '_builtin'=> false ) );
 	$names_builtin = get_post_types( array( 'public' => true, '_builtin'=> true ) );
 	$names 	= array_merge($names,$names_builtin);
 
 	if (have_posts()){ 
 	query_posts(array('category_name'=> $instance['title'],'post_type'=> $names,));
 		 while (have_posts()) : the_post(); ?>
 		 
 		<li class="category_report">
 		<span title="published:<?php esc_attr(the_time('Y年 F j日 ')); ?>"><?php echo get_post_type($post->ID); ?>&nbsp;</span>
 		<a href="<?php the_permalink(); ?>" ><?php the_title("",""); ?></a>
 		</li>
 
 <?php endwhile;
 	}
  	echo $after_widget; 
  }
  
 
 
  /** @see WP_Widget::update */
 
 function update( $new_instance, $old_instance ) {
 	$instance['title']= strip_tags(stripslashes($new_instance['title']));
 	return $instance;
 }
  /** @see WP_Widget::form */
  function form($instance) {
  $title= esc_attr($instance['title']);
  ?>
 <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('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>
  <?php
  }
 
 
 }

[emulsion_relate_posts]