下面的代码从自定义的帖子类型“floorplan”中提取“plan type”分类法,并相应地显示帖子,效果很好。我面临的问题是,只有当每篇文章只有一个分类法时,它才起作用。当我向自定义帖子添加多个分类法时,分类法过滤器不再工作。
例如,房屋的平面类型为<;1500平方英尺,>1500平方英尺,>2500平方英尺,以及所有计划。目前,如果只使用其中一种分类法,它可以正常工作,但是,我希望能够将分类法“所有平面”应用于每个楼层平面,以便在过滤后,我可以看到每个帖子。
<?php
$terms = wp_get_post_terms( $post->ID, \'plan type\' );
if($terms){
// post has course_type terms attached
$course_terms = array();
foreach ($terms as $term){
$course_terms[] = $term->slug;
}
$original_query = $wp_query;
$wp_query = null;
$wp_query = new WP_Query( array(
\'post_type\' => \'floorplans\',
\'tax_query\' => array(
array(
\'taxonomy\' => \'plan type\',
\'field\' => \'slug\',
\'terms\' => $course_terms, //the taxonomy terms I\'d like to dynamically query
\'posts_per_page\' => \'-1\',
),
),
\'orderby\' => \'title\',
\'order\' => \'ASC\',
\'posts_per_page\' => \'-1\',
));
if ( have_posts() ): ?>
<ul class="gallery_list cf">
<?php
while (have_posts() ) : the_post();
$thumb = get_field(\'floorThumb\');
?>
<li>
<a href="<?php the_permalink(); ?>">
<div class="gallery_thumb">
<img src="<?php echo $thumb[\'url\']; ?>" alt="<?php echo $thumb[\'alt\']; ?>">
</div>
<div class="overlay"></div>
<h2><?php the_title(); ?></h2>
</a>
</li>
<?php endwhile; ?>
</ul>
<?php endif;
$wp_query = null;
$wp_query = $original_query;
wp_reset_postdata();
} // end if($terms)
} ?>