本当に度々すみません。
wordpressで全記事内に添付されている画像を全て取得して、その画像がどの記事の画像なのか記事と紐づけたいのですがどうしたらいいでしょうか?
アイキャッチ画像は取得しない形にしたいです。メディアから本文に添付された画像のみ。
「現状」
テンプレート内
$paged = (int) get_query_var('paged'); $args = array( 'posts_per_page' => 14, 'paged' => $paged, 'post_type' => array('post','カスタム投稿01','カスタム投稿02'), 'post_status' => 'publish', ); $the_query = new WP_Query($args); if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?> <!-- imgTextBox --> <div class="imgTextBox"> <?php $category = get_the_category(); $cat_id = $category[0]->cat_ID; $cat_name = $category[0]->cat_name; $cat_slug = $category[0]->category_nicename; ?> <?php if(has_post_thumbnail()) : $thumb = get_the_post_thumbnail(); else: $thumb = get_post_image(get_the_content()); endif; ?> <p class="textP"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a><br><span class="postDate"><?php echo get_the_date( $format, $post ); ?></span><span class="writeName"><?php the_author(); ?></span><span class="catName"><?php echo $cat_name; ?></span></p> <p class="imgP"><a href="<?php the_permalink(); ?>"><?php the_post_thumbnail($thumb); ?></a></p> </div> <!-- imgTextBox --> <?php endwhile; ?> <?php else: ?> <?php endif; ?> </div> <div class="pagination"> <?php if (have_posts() && function_exists("pagination")) { // 一覧ページネーション pagination($wp_query->max_num_pages); } ?>
「function.php」
//投稿全画像取得 function get_post_image($content="", $idx=0) { $thumb = ""; if(strlen($content) === 0) return $thumb; // imgタグのwp-image-ID クラスを検索 preg_match_all('/wp-image-(\d+)/', $content, $m); if(count($m[1]) > 0) { while(!isset($m[1][$idx])) { $idx--; if($idx < 0) break; } // $m[1][$idx] = attachment_id if($idx > -1) $thumb = wp_get_attachment_image($m[1][$idx]); } return $thumb; }
これで全記事の全ての画像を取得して、その画像がどの記事のものか記事と紐づけできると思ったのですが、
アイキャッチ画像しか取得できませんでした。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2018/10/22 02:13
退会済みユーザー
2018/10/22 06:04