現在、「プラン」というカスタム投稿を作って、カテゴリーはwpデフォルトの共通のカテゴリーを紐づけています。「投稿」に元から設定できるカテゴリを、カスタム投稿にも同じものを設定しているという意味です。(共通カテゴリではすでにカテゴリをいくつか設定済みです)
しかし、カスタム投稿の記事一覧を、そのカテゴリ毎に表示させようとしても、何も表示されないといった現状です。いか現在のコードです。
▼カスタム投稿の登録
functions
1function plan_post(){ 2 $supports_mamber = [ 3 'title', 4 'thumbnail', 5 'revisions', 6 'custom-fields', 7 ]; 8 register_post_type('plan', 9 array( 10 'label' => '投稿 ー プラン', 11 'public' => true, 12 'has_archive' => true, 13 'menu_position' => 4, 14 'supports' => $supports_mamber, 15 'taxonomies' => array('category') 16 ) 17 ); 18} 19add_action('init','plan_post'); 20
▼以下のコードでカテゴリ毎に表示させたい
index
1<?php 2 //カテゴリーごとに表示 3 $args = array( 4 'child_of' => 9 //特定の子カテゴリを指定 5 ); 6 $categories = get_categories( $args ); 7 foreach ( $categories as $category ):?> 8 <section> 9 10 <?php 11 //カテゴリ毎にカスタム投稿(plan)を一覧表示 12 $args = array( 13 'post_type' => 'plan', 14 'taxonomy' => $categories, 15 'term' => $category->slug 16 ); 17 $posts = get_posts( $args ); 18 //もし投稿があるなら 19 if($posts): ?> 20 21 <h3><?php echo $category->name; ?></h3> 22 23 <?php 24 foreach ( $posts as $post ) : 25 setup_postdata( $post ); ?> 26 <section> 27 <h2><?php the_title(); // 記事の見出し ?></h2> 28 <div><?php the_content(); // 記事の内容 ?></div> 29 </section> 30 <?php 31 endforeach; 32 ?> 33 34 <?php endif; ?> 35 36 </section> 37 <?php endforeach; wp_reset_postdata();?>
記事投稿はすでにしていますが、上記コードではエラーは出ないものの、何も表示されません。
何が原因で、どこを変えればいいのかわからず困っています、、;
ぜひご教授ください。よろしく願いします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。