Search code examples
wordpresstaxonomy

Can't get posts from taxonomy to show?


I have a wordpress taxonomy called "product". I know the template file for my taxonomy will be taxonomy-product.php However, when I use the default wordpress post loop, it shows the posts from the default "Posts" taxonomy, and not my custom one called "product".

How can I fix this? This is my code that I have placed inside of taxonomy-product.php

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="product">
<?php the_post_thumbnail();?>
<h2 class="product-title">
<a href="<?php the_permalink() ?>"><?php the_title(); ?></a>
</h2>
<a class="product-view" href="<?php the_permalink() ?>">View Product</a>
</div>
<?php endwhile; else: ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>

Solution

  • Carly, the problem you have is that you are not including the taxonomy you are wanting to loop through inside of your loop. Try this:

    <?php
    
    $args = array( 'product' => 'example-product' );
    $loop = new WP_Query( $args );
    
    while ( $loop->have_posts() ) : $loop->the_post();
    
    <div class="product">
    
        <?php the_post_thumbnail();?>
    
        <h2 class="product-title"><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2>
    
        <a class="product-view" href="<?php the_permalink() ?>">View Product</a>
    
    </div>
    
    <?php endwhile; else: ?>
    
    <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
    
    endwhile;
    
    ?>