Re: WPのギャラリーからの画像取得について
永らくありがとうございました › フォーラム › テーマ › WPのギャラリーからの画像取得について › Re: WPのギャラリーからの画像取得について
2011 年 12 月 1 日 5:34 AM
#3311
キーマスター
画像のキャプションを引っ張ってきたりとか、標準では面倒なので
get_image_by_menu_orderで利用しているwp_get_attachment_image()関数の利用をやめて
代わりにwp_get_attachment_image()をパクッテ必要な機能を追加したwp_get_attachment_image_info()関数を作成してそれを利用します。
<code>function get_image_by_menu_order($postid,$menu_order=1,$size='full') {<br /> $args = array(<br /> 'post_type' => 'attachment',<br /> 'post_mime_type' => 'image',<br /> 'post_status' => null,<br /> 'post_parent' => $postid<br /> );<br /> $attachments = get_posts( $args );<br /> if ( $attachments ) {<br /> foreach ( $attachments as $attachment ) {<br /> if ($attachment->menu_order == $menu_order){<br /> <br /> $image = wp_get_attachment_image_info( $attachment->ID, $size );<br /> return $image;<br /> }<br /> }<br /> }<br /> }<br /> function wp_get_attachment_image_info($attachment_id, $size = 'thumbnail', $icon = false, $attr = '') {<br /> <br /> $image = wp_get_attachment_image_src($attachment_id, $size, $icon);<br /> if ( $image ) {<br /> list($src, $width, $height) = $image;<br /> $hwstring = image_hwstring($width, $height);<br /> if ( is_array($size) )<br /> $size = join('x', $size);<br /> $attachment =& get_post($attachment_id);<br /> $default_attr = array(<br /> 'src' => $src,<br /> 'class' => "attachment-$size",<br /> 'alt' => trim(strip_tags( get_post_meta($attachment_id, '_wp_attachment_image_alt', true) )), // Use Alt field first<br /> 'title' => trim(strip_tags( $attachment->post_title ))<br /> );<br /> if ( empty($default_attr['alt']) )<br /> $default_attr['alt'] = trim(strip_tags( $attachment->post_excerpt )); // If not, Use the Caption<br /> if ( empty($default_attr['alt']) )<br /> $default_attr['alt'] = trim(strip_tags( $attachment->post_title )); // Finally, use the title<br /> <br /> $attr = wp_parse_args($attr, $default_attr);<br /> $attr = apply_filters( 'wp_get_attachment_image_attributes', $attr, $attachment );<br /> $attr = array_map( 'esc_attr', $attr );<br /> $html = rtrim("<img $hwstring");<br /> foreach ( $attr as $name => $value ) {<br /> $html .= " $name=" . '"' . $value . '"';<br /> }<br /> $html .= ' />';<br /> $attr['img_tag'] = $html;<br /> $attr['caption'] = trim(strip_tags( $attachment->post_excerpt ));<br /> $attr['desc'] = trim(strip_tags( $attachment->post_content ));<br /> }<br /> <br /> return $attr;<br /> }</code>
これでget_image_by_menu_orderは各要素をばらばらに連想配列で出力するようになりますのでそれらを個別利用します。
-
配列添え字
- ‘src’ : ファイルのURL
- ‘alt’ : 代替えテキスト
- ‘title’ : タイトル
- ‘caption’ : キャプション
- ‘desc’ : 説明
- ‘image_tag’ : イメージタグ(以前のget_image_by_menu_orderで出力していた内容)
先の、お問い合わせの例では、以下のように利用します。
<code><?php<br /> $image = get_image_by_menu_order(get_the_ID(), 1,array(300,9999));<br /> ?><br /> <a href="<?php echo $image['src']; ?>" title="<?php echo $image['title']; ?>"><?php echo $image['img_tag']; ?></a><br /> <div class="caption"><br /> <a href="<?php echo $image['src']; ?>"><?php echo $image['caption']; ?></a><br /> </div><br /> <div class="image-title"><?php echo $image['title']; ?></div><br /> <div class="image-desc"><?php echo $image['desc']; ?></div></code>