お世話になります。
下記のようなカスタム投稿タイプの個別ページを作成しています。
投稿タイプ:gourmet
タクソノミー:ctgourmet
エリア(area)
東京
大阪
京都
ジャンル(genre)
カフェ
ディナー
single-gourmet.phpで、現在見ている記事が所属しているターム毎に関連記事をつけたいと考えています。
lang
1〜 2<!--ここまで記事 --> 3<?php endwhile; ?> 4 5<!--ここから関連記事 --> 6<?php 7global $post; 8// 親タームを除くスタッグ名の配列を取得する。 9$children_terms = array(); 10$terms = get_the_terms( $post->ID, 'ctgourmet' ); 11foreach ( $terms as $term ) { 12 if ( $term->parent !== 0 ) { 13 $children_terms[] = $term->slug; 14 } 15} 16$args = array( 17 'numberposts' => -1, 18 'post_type' => 'gourmet', 19 'orderby' => 'rand', 20 'post__not_in' => array( $post->ID ), 21 'tax_query' => array( array( 22 'taxonomy' => 'ctgourmet', 23 'field' => 'slug', 24 'terms' => $children_terms, 25 ) ) 26); 27?> 28<?php $myPosts = get_posts($args); if($myPosts) : ?> 29<h2>関連記事</h2> 30<ul> 31<?php foreach($myPosts as $post) : setup_postdata($post); ?> 32<li><?php the_post_thumbnail(‘thumbnail’); ?></li> 33<?php endforeach; ?> 34<?php else : ?> 35<p>関連記事はまだありません。</p> 36<?php endif; wp_reset_postdata(); ?>
上記で取得するところまではこれたのですが、これを下記のようにタームごとに分けたいのですが、なかなか検索してもたどり着かずで、そもそもアプローチ方法が違っているのかと思い、質問させていただきました。
lang
1<h2>同じエリアで探す</h2> 2<!-- 3所属するエリアの子タームだけの一覧記事 4--> 5<ul> 6<li><?php the_post_thumbnail(‘thumbnail’); ?></li> 7<li><?php the_post_thumbnail(‘thumbnail’); ?></li> 8<li><?php the_post_thumbnail(‘thumbnail’); ?></li> 9</ul> 10<h2>同じジャンルで探す</h2> 11<!-- 12所属するジャンルの子タームだけの一覧記事 13--> 14<ul> 15<li><?php the_post_thumbnail(‘thumbnail’); ?></li> 16<li><?php the_post_thumbnail(‘thumbnail’); ?></li> 17<li><?php the_post_thumbnail(‘thumbnail’); ?></li> 18</ul>
何かアドバイスを頂けるとうれしいです。
どうぞよろしくお願いいたします。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。