6

I have a custom post type (CPT) which contains products, and a taxonomy that contains product terms. I need to display these terms on the page 'Products' and when clicked on a certain term, it needs to display the products which belong to the term.

By the way, when I click 'view' on a certain term, it only displays one product's title. I created the CPT and taxonomy using the CPT UI plugin.

Here is my page-products.php template file code (which lists all the products) :

<?php
/* Template Name: Products
*/
?>

 <?php get_header('header.php') ?>

 <!--Opening container or wrap outside of the loop-->
 <div class="container my-container">
 <!--start the loop-->
   <?php
   $args=array(
    'post_type' => 'product',
    'post_status' => 'publish',
    'posts_per_page' => 10,
);

$the_query = null;
$the_query = new WP_Query($args);

if( $the_query->have_posts() ) {

$i = 0;
while ($the_query->have_posts()) : $the_query->the_post();

if($i % 3 == 0) { ?>

<div class="row">

<?php
}
?>

<div class="col-md-4">
  <div class="my-inner">
    <?php the_post_thumbnail(); ?>
    <div class="title"><a href="<?php the_permalink(); ?>"><?php 
the_title(); ?></a></div>
    <?php the_excerpt(); ?>
  </div>
</div>

  <?php $i++;
  if($i != 0 && $i % 3 == 0) { ?>
    </div><!--/.row-->
    <div class="clearfix">fgfd</div>

  <?php
   } ?>

  <?php
    endwhile;
    }
    wp_reset_query();
    ?>

3
  • This happens automatically if you use the main query in the correct templates. Review the Template Hierarchy to see the required templates. Post type/taxonomy archives do not need to be, and should not be, implemented as page templates. Dec 1, 2017 at 9:51
  • Could you please elaborate? I should query the taxonomy terms in my page-products.php first right?
    – oksite
    Dec 1, 2017 at 10:06
  • No. There shouldn’t be any page templates or secondary queries. Dec 1, 2017 at 10:44

1 Answer 1

14

To get a list of your custom taxonomies, you can use the get_terms() function to create a loop:

// Get the taxonomy's terms
$terms = get_terms(
    array(
        'taxonomy'   => 'your-taxonomy',
        'hide_empty' => false,
    )
);

// Check if any term exists
if ( ! empty( $terms ) && is_array( $terms ) ) {
    // Run a loop and print them all
    foreach ( $terms as $term ) { ?>
        <a href="<?php echo esc_url( get_term_link( $term ) ) ?>">
            <?php echo $term->name; ?>
        </a><?php
    }
} 
6
  • Thanks, this works! Though I have another problem - once I click on my category, only one post title shows up. Is there a specific template I need to have which displays posts by category? Sorry if I sound stupid, but I'm lost.
    – oksite
    Dec 1, 2017 at 10:30
  • You're welcome. It's not stupid at all. This might have several reasons. One could be that you set the post per page to 1 in your "Reading" settings in the dashboard. Other issue might be with the template. Try checking the taxonomy-your_taxonomy.php or taxonomy.php if it exists. You might also have a pre_get_posts filter in your functions.php file, limiting the results.
    – Johansson
    Dec 1, 2017 at 10:38
  • Okay, creating the taxonomy template worked, once I press on the category, I get a list of my posts. But using the code in my original post , I get the same products no matter which category I click on. For example, I have a category with 3 products and when I click it I see these products, but I see the same products when I click a different category. I think I need to use the category ID right?
    – oksite
    Dec 1, 2017 at 10:44
  • 1
    Yes of course. You need to do a tax_query if you are planning to use your own code. Take a look into WP_Query class and feel free to ask a new question if the problem persists.
    – Johansson
    Dec 1, 2017 at 12:12
  • Hey Jack. I really want to thank you for all the help. I was able to get the end result I wanted. Cheers to you man! :)
    – oksite
    Dec 1, 2017 at 12:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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