Wordpress Post Type Taxonomy Template

I have a custom post type called "Products" and it has a "Product Categories" taxonomy that has categories Category 1, Category 2, etc., which again has subcategories Category 1a, Category 2a, etc. I want, when I click on category 1, it should list the subcategories Category 1a, Category 2a, etc. When you click on category 2a, it should display the products associated with the category. How to do it with wordpress?

<?php $taxonomy_name = 'al_product_cat'; $term_childs = get_term_children( $wp_query->get_queried_object_id(), $taxonomy_name ); //print_r($term_childs); foreach($term_childs as $child){ $tm = get_term_by( 'id', $child, $taxonomy_name ); ?> <div class="tax_content"> <div class="feat_thumb"></div> <div class="feat_content"> <h2><a href="<?php echo get_term_link( $child, $taxonomy_name ); ?>"><?php echo $tm->name; ?></a></h2> <p><?php echo $tm->description; ?> </p> <div class="brand_logos"> <?php $terms = get_the_terms( $wp_query->get_queried_object_id(), 'brand' ); foreach($terms as $term){ ?> <img src="<?php echo z_taxonomy_image_url($term->term_id); ?>" /> <?php } ?> </div> </div> <div class="clear"></div> </div> <?php } ?> 
+5
source share
1 answer

You can use WordPress Templates for this purpose.

Always use WP_Query () for custom post types and taxonomy.

Now create a file in your theme, for example taxonomy-al_product_cat.php , and then write the code in this file.

This file works for parent, child, and their child categories.

For example, in the taxonomy-al_product_cat.php

 <?php get_header(); $al_cat_slug = get_queried_object()->slug; $al_cat_name = get_queried_object()->name; ?> <h2><?php echo $al_cat_name; ?></h2> <?php $al_tax_post_args = array( 'post_type' => 'Your Post Type', // Your Post type Name that You Registered 'posts_per_page' => 999, 'order' => 'ASC', 'tax_query' => array( array( 'taxonomy' => 'al_product_cat', 'field' => 'slug', 'terms' => $al_cat_slug ) ) ); $al_tax_post_qry = new WP_Query($al_tax_post_args); if($al_tax_post_qry->have_posts()) : while($al_tax_post_qry->have_posts()) : $al_tax_post_qry->the_post(); ?> <a href="<?php the_permalink(); ?>"> <?php the_title(); ?> </a> <?php endwhile; endif; get_footer(); ?> 

You can read about tax_query () and get_queried_object () from these links.

Hope this helps you.

0
source

Source: https://habr.com/ru/post/1236682/


All Articles