<?php function my_random_images($count=1, $size='thumbnail'){ $args= array( 'showposts'=> $count, 'post_type'=> 'attachment', 'post_mime_type'=> 'image', 'orderby'=> 'rand', ); $images= get_posts($args); $html= '<div class="randam-attachment" style="%4$s"> <a href="%1$s" rel="post">%2$s</a><br /> %3$s </div>'; $inline_style= 'width:'.round( (98 - $count * 2) / $count ) .'%;display:inline-block;margin:1%'; foreach($images as $key=>$image){ printf( $html, get_permalink($images[$key]->post_parent), get_the_title($images[$key]->post_parent), wp_get_attachment_image($image->ID,$size ,true, array( 'style'=>'max-width:100%' ) ), $inline_style ); } } ?>
http://ja.forums.wordpress.org/topic/12727?replies=4#post-45498(@source)
サイドバーに 2枚ずつ並べたいというリクエストだったので、サイドバー用のコードをおまけ、
<?php function my_random_images_for_sidebar($count=1, $size='thumbnail'){ $args= array( 'showposts'=> $count, 'post_type'=> 'attachment', 'post_mime_type'=> 'image', 'orderby'=> 'rand', ); $images= get_posts($args); $html= '<div class="randam-attachment" style="%4$s"> <a href="%1$s" rel="post" title="%2$s">%3$s</a><br /> </div>'; /* 2枚ずつ並べる */ $columns= 2; $inline_style= 'width:'.round( (96 - $columns * 2) / $columns ) .'%;display:inline-block;margin:0 '.$columns .'%;'; foreach($images as $key=>$image){ printf( $html, get_permalink($images[$key]->post_parent), esc_attr( get_the_title($images[$key]->post_parent) ), wp_get_attachment_image($image->ID,$size ,true, array( 'style'=>'max-width:100%;height:auto;' ) ), $inline_style ); } } ?>
[do_function name=”my_random_images” first=”3″]