###■やりたいこと
複数カテゴリ選択時にだけ関連記事が表示されないので、どうにかしたいです。。
###■いまの状況
カテゴリ選択が1つだけの場合は大丈夫です。
下記のコードで、関連記事がランダムで4件ちゃんと表示されます。
(蛇足ですが、以前人気記事の表示方法を質問しましたが、ランダム表示で妥協しました。笑)
しかし、カテゴリを複数選択するとダメなんです。
■たとえば
┃記事Xでカテゴリをa,b,cと3つ選択した場合、
┃記事Yで同じカテゴリaを選択してるのに、
┃1つも表示されなくなってしまう。
┃(XもYもいずれも表示なしです。)
ということです。
原因がお分かりになる方、いらっしゃいましたらご教示いただけると幸いです。
↓functions.php↓
php
1/*-------------------------------------------*/ 2/* カスタム投稿タイプ 3/*-------------------------------------------*/ 4 5add_action( 'init', 'custum_post_type' ); 6function custum_post_type() { 7 8/* type1 */ 9 10 //type1のカスタム投稿タイプ追加 11 register_post_type( 'type1', 12 array('labels' => 13 array( 14 'name' => __( 'type1' ), 15 'singular_name' => __( 'type1' ) 16 ), 17 'public' => true, 18 'menu_position' => 5, 19 'hierarchicla' => false, 20 'has_archive' => true, 21 'supports' => array('title','editor','thumbnail', 22 'custom-fields','excerpt','author','trackbacks', 23 'comments','revisions','page-attributes') 24 ) 25 ); 26 27 //type1のタクソノミー追加 28 register_taxonomy( 29 'tag', 30 array('type1','type2'), //type2カテゴリと同じタグを使う 31 array( 32 'hierarchical' => false, 33 'label' => 'タグ', 34 'show_ui' => true, 35 'query_var' => true, 36 'rewrite' => true, 37 'singular_label' => 'タグ' 38 ) 39 ); 40 register_taxonomy( 41 'type1-category', 42 'type1', 43 array( 44 'hierarchical' => true, 45 'label' => 'カテゴリー', 46 'show_ui' => true, 47 'query_var' => true, 48 'rewrite' => true, 49 'singular_label' => 'カテゴリー' 50 ) 51 ); 52 53/* type2 */ 54 55 //type2のカスタム投稿タイプ追加 56 register_post_type( 'type2', 57 array('labels' => 58 array( 59 'name' => __( 'type2' ), 60 'singular_name' => __( 'type2' ) 61 ), 62 'public' => true, 63 'menu_position' => 6, 64 'hierarchicla' => false, 65 'has_archive' => true, 66 'supports' => array('title','editor','thumbnail', 67 'custom-fields','excerpt','author','trackbacks', 68 'comments','revisions','page-attributes') 69 ) 70 ); 71 72 //type2のタクソノミー追加 73 register_taxonomy( 74 'type2-category', 75 'type2', 76 array( 77 'hierarchical' => true, 78 'label' => 'カテゴリー', 79 'show_ui' => true, 80 'query_var' => true, 81 'rewrite' => true, 82 'singular_label' => 'カテゴリー' 83 ) 84 );
↓single.php↓
php
1/*-------------------------------------------*/ 2/* 関連記事ランダム表示 3/*-------------------------------------------*/ 4 5<div class="type1rand-wrap"> 6<?php 7 global $post; 8 $term = array_shift(get_the_terms($post->ID, 'type1-category')); 9 $args = array( 10 'numberposts' => 4, 11 'post_type' => 'type1', 12 'taxonomy' => 'type1-category', 13 'term' => $term->slug, 14 'orderby' => 'rand', 15 ); 16?> 17 18<ul class="type1rand"> 19<?php $myPosts = get_posts($args); if($myPosts) : ?> 20<?php foreach($myPosts as $post) : setup_postdata($post); ?> 21 22<li class="rand"> 23<p>関連記事</p> 24<a href="<?php the_permalink(); ?>"> 25<p class="radsumb"><?php the_post_thumbnail(); ?></p> 26<?php the_title(); ?></a> 27</li> 28<?php endforeach; ?> 29<?php else : ?></ul> 30 <p>関連記事はまだありません。</p> 31<?php endif; wp_reset_postdata(); ?> 32</div> 33
ちょっとコードが長くてすみません。
原因がお分かりになる方いらっしゃいましたら、何卒よろしくおねがいいたします。m(__)m
回答1件
あなたの回答
tips
プレビュー