XOOPS は知りませんけども、
テーマの functions.php に以下を追加すればだいたい OK◎
add_action( 'widgets_init', 'my_widgets_init' );
function my_widgets_init() {
register_widget('my_widget_thumbnail');
}
class My_Widget_Thumbnail extends WP_Widget {
function My_Widget_Thumbnail() {
$widget_ops = array('classname' => 'my_widget_thumbnail', 'description' => 'サムネイルを表示します' );
$this->WP_Widget('my_thumbnail', 'サムネイル', $widget_ops);
}
function widget( $args, $instance ) {
extract( $args );
$title = apply_filters('widget_title', empty( $instance['title'] ) ? 'サムネイル' : $instance['title'], $instance, $this->id_base);
$out = "<span>no image</span>";
$post_thumbnail = isset( $instance['post_thumbnail'] ) ? $instance['post_thumbnail'] : false;
if( $post_thumbnail ) {
query_posts( 'meta_key=_thumbnail_id&orderby=rand&posts_per_page=1' );
if ( have_posts() ) {
the_post();
$out = get_the_post_thumbnail( get_the_ID() );/* 2つめの引数を追加してサイズ指定できます */
}
} else {
query_posts( 'post_type=attachment&post_status=inherit&orderby=rand&posts_per_page=1' );
if ( have_posts() ) {
the_post();
$out = wp_get_attachment_link( get_the_ID() );/* 2つめの引数を追加してサイズ指定できます */
}
}
wp_reset_query();
echo $before_widget;
if ( $title )
echo $before_title . $title . $after_title;
?>
<ul>
<?php echo $out; ?>
</ul>
<?php
echo $after_widget;
}
function update( $new_instance, $old_instance ) {
$new_instance = (array) $new_instance;
$instance = array( 'post_thumbnail' => 0 );
foreach ( $instance as $field => $val ) {
if ( isset($new_instance[$field]) )
$instance[$field] = 1;
}
$instance['title'] = strip_tags($new_instance['title']);
return $instance;
}
function form( $instance ) {
//Defaults
$instance = wp_parse_args( (array) $instance, array( 'post_thumbnail' => false ) );
$title = esc_attr( $instance['title'] );
?>
<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>
<input class="checkbox" type="checkbox" <?php checked($instance['post_thumbnail'], true) ?> id="<?php echo $this->get_field_id('post_thumbnail'); ?>" name="<?php echo $this->get_field_name('post_thumbnail'); ?>" />
<label for="<?php echo $this->get_field_id('post_thumbnail'); ?>">アイキャッチ画像のみ</label>
</p>
<?php
}
}
細かいところはお好みでアレンジしてください。