columnとopeという2つの投稿タイプがあります。
それぞれの投稿タイプにope_tagというタームが紐づいてます。
opeという投稿タイプの記事を表示させた時に、
サイドバーにcolumnの同じタームがついている記事を5つ表示させたいです。
下記コードでためしてみましたが、うまくいきません。
アドバイスをお願いします。
<?php global $post; $terms = get_the_terms($post->ID, 'ope_tag'); var_dump($terms); foreach($terms as $term) { echo '<h1>' . $term->name . '</h1>'; $args = array( 'numberposts' => 5, 'post_type' => 'column', //カスタム投稿タイプ名 'taxonomy' => 'ope_tag', //タクソノミー名 'term' => $term->slug, //ターム名 'orderby' => 'rand', //ランダム表示 'post__not_in' => array($post->ID) //表示中の記事を除外 ); ?> <?php $myPosts = get_posts($args); if($myPosts) : ?> <?php foreach($myPosts as $post) : setup_postdata($post); ?> <p><a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('thumbnail'); ?><?php the_title(); ?></a></p> <?php endforeach; ?> <?php else : ?> <p>関連アイテムはまだありません。</p> <?php endif; wp_reset_postdata(); } ?>
「うまくできない」とは「何をしたときに」「どうなると思って」「どうなったのか」を、出ているエラーなどと併せて、具体的に記述されたほうが回答を得られやすいと思います。
Invalid argument supplied for foreach() とエラーになります。$terms = get_the_terms($post->ID, 'ope_tag');の結果が bool(false) となるので取得できてない様です。
$post->IDは採れていますか?
object(WP_Post)#6644 (25) { ["ID"]=> int(5667) ["post_author"]=> string(1) "2" ["post_date"]=> string(19) "2014-08-19 16:31:51" ["post_date_gmt"]=> string(19) "2014-08-19 07:31:51" ["post_content"]=> string(5552) " 結果は返ってるのでとれてそうです
それであれば記述に間違いは無いので、当該IDの記事にカスタムタクソノミーope_tagのタームが割り当てられていない可能性が高いです。
その通りでした。タームをつけたら表示しました。しかし、タームがついてない場合は、関連アイテムはまだありません。と表示したいのですが、それは何がおかしいのでしょうか?
回答1件
あなたの回答
tips
プレビュー