WordPressで『アップロード』『メディアライブラリーから追加』の両方の投稿画像を抽出・出力する
WordPressでHP製作をしていますが、singleページで投稿画像10枚分の
画像ギャラリーを製作したいと考えています。
下記、コードを入力した場合、アップロードから投稿した画像は抽出・出力することが
できるのですが、メディアライブラリーから追加した画像は抽出・出力することが
できません。両方どちらからも画像投稿しても抽出・出力するには、どこの部分を
修正したらいいでしょうか。
画像はimgタグではなく、divのbackground-imageで出力するため、urlを取得できるようにコードを書いています。
また、私はウェブデザイナーでPHP言語等には詳しくないのですが、
画像URLを抽出・出力する際はどのようなテンプレートタグ(関数?)を使用することが
一般的なのでしょうか?
初歩的な質問で大変恐縮ですが、製作が完全にストップしているため、
アドバイス等を頂戴できればと思います。
宜しく御願いします。
該当のソースコード
PHP
1<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> 2<article class="work"> 3 <div class="title"> 4 <h2> 5 <?php if(!empty(the_title())): ?> 6 <?php the_title();?> 7 <?php else: ?> 8 屋根修復工事 9 <?php endif; ?> 10 </h2> 11 </div> 12 <div class="images"> 13 <?php 14 $first_image = '/<img.*?src=(["\'])(.+?)\1.*?>/i'; 15 preg_match($first_image, $post->post_content, $image_url); 16 ?> 17 <?php if(!empty($image_url[2])): ?> 18 <div class="mainImg" style="background-image:url('<?php echo $image_url[2] ;?>')"> 19 <?php else: ?> 20 <div class="mainImg" style="background-image:url('<?php bloginfo('template_url'); ?>/images/noImg.jpg')"> 21 <?php endif; ?> 22 <div class="cat"><?php the_category(); ?></div> 23 </div> 24<!-- ここまでサムネイル画像の出力 --> 25 <?php 26 // この投稿の添付ファイル(画像)を取得 27 $attachments = get_children(array( 28 'post_parent' => $post->ID, // この投稿のID 29 'post_status' => 'inherit', // 公開ステータス(post_parentに準じる) 30 'post_type' => 'attachment', //投稿タイプ(添付ファイル) 31 'post_mime_type' => 'image', // MIMEタイプ(画像) 32 'orderby' => 'menu_order', // ソート(任意) 33 'order' => 'ASC' // 並び順(昇順) 34 )); 35 ?> 36 <ul class="imglist"> 37 <?php 38 $i=1; 39 foreach ( $attachments as $attachment_id => $attachment ) { 40 $thumb_image = wp_get_attachment_link($attachment_id, array(700,700)); 41 echo '<li class="navImg">'.$thumb_image.'</li>'."\n"; 42 $i++; 43 } 44 ?> 45 </ul> 46 </div> 47 <div class="clear"></div> 48 <div class="ankenmei"> 49 <p class="namae">案件名</p> 50 <p class="title"> 51 <?php if(!empty(the_title())): ?> 52 <?php the_title();?> 53 <?php else: ?> 54 屋根修復工事 55 <?php endif; ?> 56 </p> 57 </div> 58 <div class="point"> 59 <p class="namae">Point</p> 60 <p class="title"><?php $content = get_the_content(); 61 $content = wp_strip_all_tags( $content ); 62 echo $content; ?></p><!-- 本文テキストのみ出力 --> 63 </div> 64 <?php 65 function get_first_post_image_url() { 66 if(!is_single()) return; 67 global $post; 68 preg_match('/<img.+src=[\'"](^\'"]+)[\'"].*>/', $post->post_content, $matches); 69 return $matches[1]; 70 } 71 ?> 72</article> 73<?php endwhile; else : ?> 74 <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p> 75<?php endif; ?>
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。