rtr-x8
10/11/2017 - 2:30 AM

同一カテゴリループ

同じカテゴリでループさせる。 本記事は表示しない。 複数カテゴリ対応

<?php
  $posttype   = get_post_type();
	$taxonomy   = 'tax_' , $posttype;
	$terms      = get_the_terms($post->ID, $taxonomy);
	$slug_array = array();
	foreach ($terms as $term) {
		array_push($slug_array, $term->slug);
	};
	$args = array(
		'post_type'      => $posttype,
		'posts_per_page' => 6,
		'post__not_in'   => array($post->ID),
		'tax_query'      => array( 
			array(
				'taxonomy'  => $taxonomy,
				'field'     => 'slug',
				'terms'     => $slug_array,
			),
		),
	);
	$the_query = new WP_Query($args);
	if( $the_query->have_posts() ):
?>

<div class="relatedPost">
	<p>同じカテゴリの投稿。ひとつでも被れば出力</p>
	<ul class="">
	<?php
		while($the_query->have_posts()) {
			$the_query->the_post();
			echo '<li class="grid_item">';
			include(TEMPLATEPATH  . '/inc/parts-postCard02.php');
			echo '</li>';
		};
	?>		
	</ul>
</div>

<?php endwhile; ?>