お世話になります。
[single-faq.php]に同じタームの関連記事を表示したいと思っています。
php
1<?php 2 global $post; 3 $term = array_shift(get_the_terms($post->ID, 'faq')); 4 $args = array( 5 'numberposts' => 8, //8件表示 6 'post_type' => 'faq, //カスタム投稿タイプ名 7 'taxonomy' => 'faq-cat', //タクソノミー名 8 'term' => $term->slug, //ターム名 9 'orderby' => 'rand', //ランダム表示 10 'post__not_in' => array($post->ID) //表示中の記事を除外 11 ); 12?> 13<?php $myPosts = get_posts($args); if($myPosts) : ?> 14<?php foreach($myPosts as $post) : setup_postdata($post); ?> 15 <p><a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('thumbnail'); ?><?php the_title(); ?></a></p> 16<?php endforeach; ?> 17<?php else : ?> 18 <p>関連アイテムはまだありません。</p> 19<?php endif; wp_reset_postdata(); ?>
これだと、同じタームではなくすべてのfaqの記事が対象になってしまい困っております。
可能であれば、この記事に複数のタームが選択されていれば、その複数のタームの関連記事を表示できればと思っております。
おわかりでしたら、お力貸していただければ幸いです。
宜しくお願いいたします。
↓動作したコードはこちら
php
1<?php 2 global $post; 3//$term = array_shift(get_the_terms($post->ID, 'faq')); 4 $term = array_shift(get_the_terms($post->ID, 'faq-cat')); // タクソノミー名を指定する 5 $args = array( 6 'numberposts' => 8, //8件表示(デフォルトは5件) 7 'post_type' => 'faq', //カスタム投稿タイプ名 8 'taxonomy' => 'faq-cat', //タクソノミー名 ←ここが追加 9 'term' => $term->slug, //ターム名 10 'orderby' => 'rand', //ランダム表示 11 'post__not_in' => array($post->ID) //表示中の記事を除外 12 ); 13?> 14<?php $myPosts = get_posts($args); if($myPosts) : ?> 15<?php foreach($myPosts as $post) : setup_postdata($post); ?> 16 <p><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></p> 17<?php endforeach; ?> 18<?php else : ?> 19 <p>関連アイテムはまだありません。</p> 20<?php endif; wp_reset_postdata(); ?> 21 <!--ここまで--> 22
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/12/14 05:52
2016/12/14 06:12
退会済みユーザー
2016/12/14 06:13
退会済みユーザー
2016/12/14 06:14
2016/12/14 06:16