記事に挿入された画像のURLを取得。
この記事は2011/07/25に公開され2012/01/09に更新、1,353回読まれました。
WebTecNoteさんの「Wordpress2.7 記事に添付された画像を取得して表示」という記事を読み利用させていただきました。
http://tenderfeel.xsrv.jp/wordpress/513/
プログラム詳細はWebTecNoteさんをご覧ください。
ここでは最終行のwp_get_attachment_imageをwp_get_attachment_image_srcとして、画像情報を配列で取得しています。
- <?php
- function get_the_post_image_src($postid,$size,$order=0,$max=null) {
- $attachments = get_children(array(‘post_parent’ => $postid, ‘post_type’ => ‘attachment’, ‘post_mime_type’ => ‘image’));
- if ( is_array($attachments) ){
- foreach ($attachments as $key => $row) {
- $mo[$key] = $row->menu_order;
- $aid[$key] = $row->ID;
- }
- array_multisort($mo, SORT_ASC,$aid,SORT_DESC,$attachments);
- $max = empty($max)? $order+1 :$max;
- for($i=$order;$i<$max;$i++){
- return wp_get_attachment_image_src( $attachments[$i]->ID, $size );
- }
- }
- }
- ?>
上記をfunctions.php最終行(適当なところでOK)に追加してください。
使い方は
- <?php
- $data=get_the_post_image_src(get_the_ID(),”full”);
- echo $data[0];
- ?>
配列[0]には画像のURLが入ります。
コメントする