サポート » その他 » カテゴリーを取得して関連記事を表示する方法について

  • 解決済 nycstadium

    (@nycstadium)


    お世話になっております。
    現在下記のテーマを使用しています。

    http://lionmedia.fit-jp.com/matome/

    記事の下部に関連タグで関連記事が表示される仕組みになっているのですが、
    カテゴリーをもとに関連記事を表示させる場合、どのようにコードを書き直せばいいでしょうか?

    記事には、BIG>MID>SMALLのように3段階のカテゴリーを設定しております。
    この「親カテゴリーのBIG」をもとに関連記事を表示させたいです。

    個別投稿(single.php)にあります、下記の部分が関連記事に関するコードです。
    PHPの知識がなくアドバイスお願いします。

    	  <?php if (get_option('fit_post_related') != 'value2' ) : ?>
          <!-- 関連記事 -->
    	  <?php
    	  // 総件数
    	  if(get_option('fit_post_relatedNumber')){
    		  $max_post_num = get_option('fit_post_relatedNumber');
    	  }else{
    		  $max_post_num = 3;
    	  }
    	  // 現在の記事にタグが設定されている場合
    	  if ( has_tag() ) {
    	      // 1.タグ関連の投稿を取得
    	      $tags = wp_get_post_tags($post->ID);
    	      $tag_ids = array();
    	      foreach($tags as $tag):
    	          array_push( $tag_ids, $tag -> term_id);
    	      endforeach ;
    	      $tag_args = array(
    	          'post__not_in' => array($post -> ID),
    	  		  'tag__not_in' => $tag_ids,
    	          'posts_per_page'=> $max_post_num,
    	          'tag__in' => $tag_ids,
    	          'orderby' => 'rand',
    	      );
    	      $rel_posts = get_posts($tag_args);
     	     // 総件数よりタグ関連の投稿が少ない場合は、カテゴリ関連の投稿からも取得する
    	      $rel_count = count($rel_posts);
    	      if ($max_post_num > $rel_count) {
    	  		$categories = get_the_category($post->ID);
    	          $category_ID = array();
    	          foreach($categories as $category):
    	              array_push( $category_ID, $category -> cat_ID);
    	          endforeach ;
    	          // 取得件数は必要な数のみリクエスト
    	          $cat_args = array(
    	              'post__not_in' => array($post -> ID),
    	              'posts_per_page'=> ($max_post_num - $rel_count),
    	              'category__in' => $category_ID,
    	              'orderby' => 'rand',
     	         );
     	         $cat_posts = get_posts($cat_args);
    	          $rel_posts = array_merge($rel_posts, $cat_posts);
    	      }
    	  } else { // 現在の記事にタグが設定されていない場合
    
    	      $categories = get_the_category($post->ID);
    	      $category_ID = array();
    	      foreach($categories as $category):
    	          array_push( $category_ID, $category -> cat_ID);
    	      endforeach ;
    	      // 取得件数は必要な数のみリクエスト
    	      $cat_args = array(
    	          'post__not_in' => array($post -> ID),
    	          'posts_per_page'=> ($max_post_num),
    	          'category__in' => $category_ID,
    	          'orderby' => 'rand',
    	      );
    	      $cat_posts = get_posts($cat_args);
     	      $rel_posts = $cat_posts;
    	  }
    
    	  echo'<aside class="related">';
    	  echo'<h2 class="heading heading-primary">関連する記事</h2>';
    
    	  // 1件以上あれば
    	  if( count($rel_posts) > 0 ){
    	  	echo'<ul class="related__list">';
    	  	if (!$myAmp && is_active_sidebar( 'related-ad' ) ){
    	  		dynamic_sidebar( 'related-ad' );
    	  	}
    	  	foreach ($rel_posts as $post){setup_postdata($post);
    		// thumbnailサイズの画像内容を取得
    		$thumbnail_id = get_post_thumbnail_id(); 
    		$thumb_img = wp_get_attachment_image_src( $thumbnail_id , 'thumbnail' );
    		// サムネイル画像出力
    		$thumb_src = $thumb_img[0];
    		$thumb_width = $thumb_img[1];
    		$thumb_height = $thumb_img[2];
    		?>
    	      <li class="related__item">
    	        <a class="related__imgLink" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
    			<?php if(has_post_thumbnail()) : ?>
    		      <?php if($myAmp){echo '<amp-img layout="responsive"';}else{echo '<img';} ?> src="<?php echo $thumb_src; ?>" alt="<?php the_title(); ?>" width="<?php echo $thumb_width; ?>" height="<?php echo $thumb_height; ?>" >
    			  <?php if($myAmp){echo '</amp-img>';}?>
    		    <?php else :?>
                  <?php if($myAmp){echo '<amp-img layout="responsive"';}else{echo '<img';} ?> src="<?php echo get_template_directory_uri(); ?>/img/img_no_thumbnail.gif" alt="NO IMAGE" width="160" height="160" >
    			  <?php if($myAmp){echo '</amp-img>';}?>		
    		    <?php endif; ?>
    	        </a>
    	        <h3 class="related__title">
    	          <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
    	          <?php if (get_option('fit_post_time') != 'value2' ) :	?>
                  <span class="icon-calendar"><?php the_time('Y.m.d'); ?></span>
                  <?php endif; ?>
    	        </h3>
    	        <p class="related__contents"><?php echo mb_substr(get_the_excerpt(), 0, 75); ?>[…]</p>
    	      </li>
    	  	<?php }
    	
    	  	echo'</ul>';
    	  }else{
    	  	echo'<p class="related__contents related__contents-max">関連記事はありませんでした</p>';
    	  }
     
    	  echo'</aside>';
    	  ?>
    	  <?php wp_reset_postdata();?>
          <!-- /関連記事 -->
    	  <?php endif; ?>
1件の返信を表示中 - 1 - 1件目 (全1件中)
  • トピック投稿者 nycstadium

    (@nycstadium)

    投稿するカテゴリーを間違えてしまいましたのでカテゴリーを修正し再投稿させていただきます。
    大変失礼いたしました。

1件の返信を表示中 - 1 - 1件目 (全1件中)
  • トピック「カテゴリーを取得して関連記事を表示する方法について」には新たに返信することはできません。