実現したいこと
Wordpress カスタム投稿の記事一覧に、その記事が持つカテゴリーを表示させたい。
以下のようにループの処理を書きました。
カテゴリーのID 2,3,4を取得したく 'category__in' => array(2, 3, 4 ), と書いたのですが、これだと「投稿が存在しません。」となってしまいます。
'category__in' => array(2, 3, 4 ),の記述を消すと、'post_type' => 'news',の記事がすべて取得、表示されるので、カテゴリーのID 2,3,4を取得したく追加した 'category__in' => array(2, 3, 4 ),がうまく動作していないのですが、どうすればカテゴリー名を取得し、一覧内でその記事が持つカテゴリーを表示できますか?
archive.php
1<?php 2$args = array( 3 'post_type' => 'news', 4 'order' => 'DESC', 5 'category__in' => array(2, 3, 4 ), 6); 7 8$the_query = new WP_Query($args); 9if ($the_query->have_posts()) : 10?> 11 <!-- ループ前の開始タグ --> 12 <?php while ($the_query->have_posts()) : $the_query->the_post(); ?> 13 <!-- posts_per_page: -1 --> 14 <li class="news-list__item"> 15 <div class="news-list-item"> 16 <div class="news-list-item__category"> 17 <p class="label-category -news"><?php the_category(", "); ?></p> 18 </div> 19 <div class="news-list-item__date"> 20 <p class="label-date"><?= get_the_time('Y.m.d', $id); ?></p> 21 </div> 22 <div class="news-list-item__title"> 23 <p class="text"><a href=<?php the_permalink(); ?>><?= get_the_title(); ?></a></p> 24 </div> 25 </div> 26 </li> 27 <?php 28 endwhile; 29 ?> 30<?php 31else : 32?> 33 <div class="c-text-box u-text-align-center-pc"> 34 <p>投稿が存在しません。</p> 35 </div> 36<?php 37endif; 38wp_reset_postdata(); 39?>

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。