尝试了至少5个这样的答案,但对我来说没有任何效果。我有此代码,我想输出该类别内的所有产品以及顶部的类别名称:
<?php $params = array(
\'post_type\' => \'product\',
\'tax_query\' => array(
array(
\'taxonomy\' => \'product_cat\',
\'field\' => \'slug\', //This is optional, as it defaults to \'term_id\'
\'terms\' => array(\'bronchoscopes\'),
\'operator\' => \'IN\' // Possible values are \'IN\', \'NOT IN\', \'AND\'.
),
)
);
$products = new WP_Query($params);
?>
<?php if ($products->have_posts()) : // (3) ?>
<?php while ($products->have_posts()) : // (4)
$products->the_post(); // (4.1) ?>
<?php the_title(); // (4.2) ?>
<?php echo single_term_title(); ?>
<?php echo the_category(); ?>
<?php endwhile; ?>
<?php wp_reset_postdata(); // (5) ?>
<?php else: ?>
<p>
<?php _e( \'No Products\' ); // (6) ?>
</p>
<?php endif; ?>
但我在页面上只看到了我的两个产品,而不是类别名称本身。怎么回事?