我对您的代码进行了一些调整,以集成wp_query()
类而不是query posts()
, 这是only meant for altering the main loop. 您应该始终选择使用wp_query()
尝试创建次循环时。
因为我们正在使用wp_query()
, 我们还必须使用wp_reset_postdata()
而不是wp_reset_query
. 我不确定这是否能解决您的问题,但请调整您的代码以适应此情况,我们将逐步解决您的其余问题。
<?php $auto_types = get_terms(\'type\', \'hide_empty=1\'); ?>
<ul>
<?php foreach( $auto_types as $auto_type ) : ?>
<li>
<a href="<?php echo get_term_link( $auto_type->slug, \'type\' ); ?>">
<?php echo $auto_type->name; ?>
</a>
<?php $auto_brands = get_terms(\'brand\', \'parent=0\' ); ?>
<ul>
<?php foreach ($auto_brands as $auto_brand) : ?>
<?php $brand_filter[\'tax_query\'] = array(
\'relation\' => \'AND\',
array(
\'taxonomy\' => \'type\',
\'terms\' => array($auto_type->slug),
\'field\' => \'slug\',
),
array(
\'taxonomy\' => \'brand\',
\'terms\' => array($auto_brand->slug),
\'field\' => \'slug\',
),
);
$tax_query = new WP_Query($brand_filter);
if ( $tax_query->have_posts() ) :
$count = 1;
while ( $tax_query->have_posts() ) :
$tax_query->the_post();
if ( $count >= 1 ) { ?>
<li>
<a href="/?type=<?php echo $auto_type->slug ?>&brand=<?php echo $auto_brand->slug ?>">
- - <?php echo $auto_brand->name; ?> (<?php echo $count; ?>)
</a>
</li>
<? }
$count++;
endwhile;
wp_reset_postdata();
endif;
endforeach
?>
</ul>
</li>
<?php endforeach ?>
</ul>
UPDATE: 我添加了
posts_per_page
参数并将其设置为
-1
显示所有帖子。我在我这边测试了一下。它应该会给你你想要的结果。
<?php $auto_types = get_terms(\'type\', \'hide_empty=1\'); ?>
<ul>
<?php foreach( $auto_types as $auto_type ) : ?>
<li>
<a href="<?php echo get_term_link( $auto_type->slug, \'type\' ); ?>">
<?php echo $auto_type->name; ?>
</a>
<?php $auto_brands = get_terms(\'brand\', \'parent=0\' ); ?>
<ul>
<?php foreach ($auto_brands as $auto_brand) : ?>
<?php $brand_filter = array(
\'posts_per_page\' => \'-1\',
\'tax_query\' => array(
\'relation\' => \'AND\',
array(
\'taxonomy\' => \'type\',
\'field\' => \'slug\',
\'terms\' => array($auto_type->slug),
),
array(
\'taxonomy\' => \'brand\',
\'field\' => \'slug\',
\'terms\' => array($auto_brand->slug),
)
)
);
$tax_query = new WP_Query($brand_filter);
$count = 0;
if ( $tax_query->have_posts() ) : while ( $tax_query->have_posts() ) : $tax_query->the_post();
$count++;
endwhile; endif; wp_reset_postdata();
if ( $count > 0 ) : ?>
<li>
<a href="/?type=<?php echo $auto_type->slug ?>&brand=<?php echo $auto_brand->slug ?>">
- - <?php echo $auto_brand->name; ?> (<?php echo $count; ?>)
</a>
</li>
<?php endif; endforeach ?>
</ul>
</li>
<?php endforeach ?>
</ul>