get_postsのパラメータ範囲指定
-
以前こちらのフォーラムで、全記事中で使われている画像から任意の数をランダムに選んで、その画像が使われている記事への画像付きリンクを生成する関数が紹介されていました。
以下がそのソースコードです。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 ); } }
現在こちらを流用して、サイドバーなどに画像付きリンクを並べているのですが、ひとつ問題があります。
こちらのコード、記事に関連付けられていない画像(例えばファビコンやブログの背景画像)まで拾ってしまいます。
post_parentが0以外の画像を抽出できればよいのですが、get_postsのパラメータにて、そういった指定は可能でしょうか?
2件の返信を表示中 - 1 - 2件目 (全2件中)
2件の返信を表示中 - 1 - 2件目 (全2件中)
- トピック「get_postsのパラメータ範囲指定」には新たに返信することはできません。