I have a WP_Query that outputs featured products. For each product I would also like to link to the category of that product, but I'm not sure how.

I think this is close to being my answer, but I'm not sure how to put it all together. How to get category link without a database query

My query so far is:

  $args = array(
    'post_type' => 'product',
    'tax_query' => array(
        'taxonomy' => 'product_visibility',
        'field'    => 'name',
        'terms'    => 'featured',
        'operator' => 'IN'
    'posts_per_page' => 8
  $loop = new WP_Query( $args ); ?>

<ul class="product-list">

  <?php while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
  <li class="product-list__item">

    <a href="<?php echo get_permalink( $loop->post->ID ) ?>"
      <?php the_title(); ?>

    Link to the product category here!

Any help is much appreciated.

1 Answer 1


You can use get_the_term_list() to output a comma-separated list of links to product categories:

<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
    <li class="product-list__item">
        <a href="<?php the_permalink() ?>">
            <?php the_title(); ?>

        <?php echo get_the_term_list( get_the_ID(), 'product_cat', '', ', ' ); ?>
<?php endwhile; ?>

Note that when you're inside 'the loop' (i.e. between $loop->the_post(); and endwhile) you don't need to pass an ID to get_permalink(), you can just use the_permalink().

Also, in your code you're missing the last > from the opening anchor tag, make sure to fix that.

  • 1
    Thank you, this works great. Also thanks for the tip on the_permalink. Much appreciated. Jul 25, 2017 at 12:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.