0
votes

I have created a custom post type Products using CPT plugin, then I created three custom taxonomies to be used when adding new products, let's say they're Brands, Usage and Types.

What I'm trying to achieve is to have a page that lists all terms inside a custom taxonomy which, on click, takes you to a single term page which lists all Products tagged with that term. Finally clicking on a Product takes you to a single product page.

Basically I want this: Home -> Taxonomy listing -> Single term -> Product

This is all extremely new and uncharted to me. I came across using taxonomy.php to create a custom archive for my taxonomies, but I have no idea how to make it all work or what my file structure should look like. What templates should I use and how to connect them. Also, since taxonomy terms are not posts, how can I create a single page for each and every term in my taxonomies?

From what I've realised I need a page for my Taxonomy listing, then I can use a taxonomy.php to template single term pages. I can't figure out the step necessary to link a term in the list with a template file.

Any help would be greatly appreciated. Thanks!

1

1 Answers

1
votes

I usually create a custom page template for taxonomy term listings. Inside that template, you could read the terms of your taxonomy:

<?php 
$terms = get_terms( $taxonomy ); 
foreach( $terms as $term ) :
?>
<a href="<?php echo get_term_link( $term, $taxonomy ); ?>">
<?php echo $term->name; ?>
</a>
<?php endforeach; ?>

Then, inside your taxonomy.php, you could use a simple loop to display all products:

<?php while( have_posts() ) : the_post(); ?>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<?php endwhile(); ?>

Then all you need is a single.php to display your product details:

<?php if ( have_posts() ) : the_post(); ?>
<!-- Show post details here -->
<?php endif; ?>

Helpful overview: https://codex.wordpress.org/images/1/18/Template_Hierarchy.png