How to echo woocommerce category name

Question

Tried at least 5 SO answers, but nothing is working for me. I have this code and I want to output all the products within the category together with the category name on top:

<?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; ?>

But I’m getting only my two products on the page, not the category name itself. What’s the deal?

0
, , , Limpuls 6 years 2018-02-09T09:04:45-05:00 0 Answers 72 views 0

Leave an answer

Browse
Browse