前提
WordPressのオリジナルテーマでホームページを作っています。
実現したいこと
ここに実現したいことを箇条書きで書いてください。
- 通常の投稿とは別にプラグイン(Custom Post Type UI)を使用しカスタム投稿ページを作成しています。
- 商品の一覧ページを作成したいのですが、まずはカスタムタクソノミーごとの一覧が表示されるようにし、最終的にはカスタムタクソノミーごとに絞れるようにしたいです。
発生している問題・エラーメッセージ
Custom Post Type UIを使用し、カスタム投稿タイプとカスタムタクソノミーを作成しました。 コードの中で作成したslugを指定しているのですが、一覧ページに表示されません。
該当のソースコード
PHP
1<?php get_header('service'); ?> 2 3<main id="main"> 4 <div class="main"> 5 6 <!-- service-archive --> 7 <section id="service-archive"> 8 <div class="service-archive"> 9 <div class="inner"> 10 11 <!-- service-list --> 12 <section id="service-list"> 13 <ul class="service-list"> 14 <?php 15 wp_list_categories(array( 16 'title_li' => '', //デフォルトで出力されるタイトルを非表示 17 )); 18 ?> 19 </ul> 20 </section> 21 22 <!-- service-item --> 23 <section id="service-item"> 24 <div class="service-item"> 25 26 <div class="raw-milk"> 27 <h3></h3> 28 </div> 29 30 <!-- MILK --> 31 <div id="milk"> 32 <h2>牛乳<br> 33 <span>MILK</span> 34 </h2> 35 <ul> 36 <?php 37 $custom_posts = get_posts(array( 38 'post_type' => 'service', // 投稿タイプ 39 'posts_per_page' => 6, // 表示件数 40 'orderby' => 'date', // 表示順の基準 41 'order' => 'DESC', // 昇順・降順 42 'tax_query' => array( 43 array( 44 'taxonomy' => 'milk', //タクソノミーを指定 45 'field' => 'slug', //ターム名をスラッグで指定する 46 'terms' => 'タームスラッグ', //表示したいタームをスラッグで指定 47 'operator' => 'IN' 48 ), 49 ) 50 )); 51 global $post; 52 if ($custom_posts) : foreach ($custom_posts as $post) : setup_postdata($post); ?> 53 54 <li> 55 <a href="<?php the_permalink(); ?>"> 56 <figure> 57 <?php if (has_post_thumbnail()) : ?> 58 <img src="<?php the_post_thumbnail_url('full'); ?>"> 59 <?php endif; ?> 60 </figure> 61 </a> 62 <a href="<?php the_permalink(); ?>"> 63 <h3><?php the_title(); ?></h3> 64 </a> 65 <p><?php the_content(); ?></p> 66 <button class="btn01"> 67 <a href="<?php the_permalink(); ?>">詳細はこちら</a> 68 </button> 69 </li> 70 71 <?php endforeach; 72 wp_reset_postdata(); 73 endif; ?> 74 75 </ul> 76 </div> 77 78 <!-- YOGURT --> 79 <div id="yogurt"> 80 <h2>ヨーグルト<br> 81 <span>YOGURT</span> 82 </h2> 83 <ul> 84 <?php if (have_posts()) : while (have_posts()) : the_post(); ?> 85 <li> 86 <a href="<?php the_permalink(); ?>"> 87 <figure> 88 <?php if (has_post_thumbnail()) : ?> 89 <img src="<?php the_post_thumbnail_url('full'); ?>"> 90 <?php endif; ?> 91 </figure> 92 </a> 93 <a href="<?php the_permalink(); ?>"> 94 <h3><?php the_title(); ?></h3> 95 </a> 96 <p><?php the_content(); ?></p> 97 <button class="btn01"> 98 <a href="<?php the_permalink(); ?>">詳細はこちら</a> 99 </button> 100 </li> 101 <?php endwhile; 102 endif; ?> 103 </ul> 104 </div> 105 106 <!-- MAIRY BEVERAGE --> 107 <div id="mairy-beverage"> 108 <h2>乳飲料<br> 109 <span>MAIRY BEVERAGE</span> 110 </h2> 111 <ul> 112 <?php if (have_posts()) : while (have_posts()) : the_post(); ?> 113 <li> 114 <a href="<?php the_permalink(); ?>"> 115 <figure> 116 <?php if (has_post_thumbnail()) : ?> 117 <img src="<?php the_post_thumbnail_url('full'); ?>"> 118 <?php endif; ?> 119 </figure> 120 </a> 121 <a href="<?php the_permalink(); ?>"> 122 <h3><?php the_title(); ?></h3> 123 </a> 124 <p><?php the_content(); ?></p> 125 <button class="btn01"> 126 <a href="<?php the_permalink(); ?>">詳細はこちら</a> 127 </button> 128 </li> 129 <?php endwhile; 130 endif; ?> 131 </ul> 132 </div> 133 134 </div> 135 </section> 136 </div> 137 </div> 138 </section> 139 </div> 140</main> 141 142<?php get_footer(); ?>
試したこと
『Custom Post Type UI』で「service」というカスタム投稿を作成し
「service」というタクソノミーを作成してその中で「milk」、「yogurt」などのカテゴリーを作成しました。
上記のコードで投稿タイプを「service」とし、タクソノミーを「milk」としましたが
コードを記述している部分はデータが何も表示されない状態です。
『Custom Post Type UI』の設定でアーカイアブありを「真」にするなど
調べながら設定してみましたが解決できませんでした。
補足情報(FW/ツールのバージョンなど)
WordPressのプラグイン『Custom Post Type UI』を使用しています。
回答1件
あなたの回答
tips
プレビュー