我有一个名为Developments的自定义帖子类型。他们是建筑物。他们有两种不同的分类:城市和地位。在其中一个建筑物的City taxonomy页面上,我试图显示与Status分类相关的所有帖子。
<?php
// Vars
$status_taxonomy = 'development_status';
$devs = get_terms( array(
'taxonomy' => $status_taxonomy,
'hide_empty' => true,
) );
?>
<?php
foreach($devs as $dev) :
$dev_id = $dev->term_id;
$dev_name = $dev->name;
?>
<?php
$term_slug = $dev->slug;
$dev_posts = new WP_Query( array(
'post_type' => 'developments',
'posts_per_page' => -1, //important for a PHP memory limit warning
'tax_query' => array(
array(
'taxonomy' => $status_taxonomy,
'field' => 'slug',
'terms' => $term_slug,
'operator' => 'IN',
),
),
));
if( $dev_posts->have_posts() ) :
echo '<h3>'. $dev_name .'</h3>';
while ( $dev_posts->have_posts() ) : $dev_posts->the_post();
?>
<div class="col-xs-12">
<h3>$dev_name</h3>
</div>
<div class="col-md-4">
<h4><?php the_title(); ?></h4>
</div>
<?php
endwhile;
endif;
wp_reset_postdata();
?>
<?php
endforeach;
?>
此代码输出所有状态字词,但它也显示了所有帖子,我需要它来显示与City关联的帖子。我不知道如何修改上面的代码来实现这一点。
答案 0 :(得分:1)
在City taxonomy页面上获取您当前的分类和术语,如下所示:
$current_term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
然后更改您tax_query
以添加另一个数组,以便将当前城市分类添加到限制中:
'tax_query' => array
(
'relation' => 'AND',
array(
'taxonomy' => $status_taxonomy,
'field' => 'slug',
'terms' => $term_slug,
'operator' => 'IN',
),
array(
'taxonomy' => 'city',
'field' => 'slug',
'terms' => $current_term->slug,
'operator' => 'IN',
),
),