wordpressで全ての記事内の画像を取得して一覧で表示し、その画像がどの記事の画像なのか
タイトルやパーマリンクで紐付けるコードを、先程質問しまして教えて頂きました。
そのコードを少し改良しまして、自分に使い良いものにしまいsた。
とてもありがたかったのですが、
例えば、Aという記事に20枚画像があり、記事の表示件数が14件で、ページャーは14件表示されたら
次のページのリンクを表示、記事は全てで20件会った場合、
Aの記事に何枚画像があろうが1ページに記事内の全部の画像(この場合20枚)の一覧を表示したのち、別の記事を13件一覧で表示して、合わせて14件の記事を表示してから
次のページのページャーが表示されてしまいます。
Aの記事は画像が20枚あってもそれは全部Aの記事の画像なので、20枚全部で1記事としてカウントされてしまうようです。
それをやめて、記事と記事に添付されている画像のヒモ付はそのままにして、
画像の枚数によってページャーのページ数を増やす方法はありますでしょうか?
うまく説明できなくてすいません。
記事が14件を超えたら次のページのページャーが出るよになっている。でも一番上の記事の画像が9枚あるので
長くなっている。
<div class="newPostBox"> <?php $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(); ?> <div><?php $output = preg_match_all('/<img.+?src=[\'"]([^\'"]+)[\'"].*?>/i', $post->post_content, $matches); $all_img = $matches[1]; if( !empty( $all_img) ) { foreach( $all_img as $img ) { ?> <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; ?> <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 echo '<img src="'.$img.'">' ?></a></p> </div> <?php } } ?> </div> <?php endwhile; wp_reset_postdata(); else: ?> <!-- 投稿が無い場合の処理 --> <?php endif; ?> </div> <div class="pagination"> <?php if ($the_query->max_num_pages > 1) { echo paginate_links(array( 'base' => get_pagenum_link(1) . '%_%', 'format' => 'page/%#%/', 'current' => max(1, $paged), 'total' => $the_query->max_num_pages )); } ?> </div> <?php wp_reset_postdata(); ?>
回答1件
あなたの回答
tips
プレビュー