So, I need to show a page with the sub-categories of the current custom taxonomy, with their respective posts inside.
What I need is, when I click on a sub-cat link, I get something like:
'Tipos' (custom taxonomy)
- Auto (first sub-cat)
- Sub-Item 1 (first sub-sub-cat)
- Post 1
- Post 2
- Sub-Item 2 (second sub-sub-cat)
- Post 1
- Post 2
I have another sub-cats, but I dont wanna see them, since I clicked to see the sub-sub-cats inside the sub-cat 'auto'.
The name of the Custom Post Type is 'Produtos'
The name of the custom taxonomy is 'Tipos'
The page I'm using is 'taxonomy-tipos.php
I know I need to:
- Get the current custom taxonomy;
- Make a loop that shows a list of the sub-categories of that custom taxonomy;
- Inside the list of sub-categories, show the posts inside each sub-category;
I understand about WordPress hierarchy, but the problem is that I need to automatically show those sub-categories based on the taxonomy, and I'm not being able to achieve that.
With my code, I get all the categories inside the custom taxonomy. I wanna get only the ones inside the current one.
This is the code I tried:
Removed the code so it wont confuse anyone
Can you guys give me a light? Thanks.
PROBLEM SOLVED. FINAL CODE (WORKING):
$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
echo $term->name;
$queried_object = get_queried_object();
$term_id = get_queried_object()->term_id;
$taxonomyName = "tipos";
$termchildren = get_term_children( $term_id, $taxonomyName );
if ($termchildren != false){
foreach ($termchildren as $child) {
$term2 = get_term_by( 'id', $child, $taxonomyName );
echo $term2->name;
$my_query = new WP_Query( array(
'post_type' => 'produtos',
'posts_per_page' => -1,
'orderby' => 'menu_order',
'order' => 'DESC',
'tax_query' => array(
array(
'taxonomy' => $taxonomyName,
'field' => 'slug',
'terms' => $term2->slug,
)
),
));
while ($my_query->have_posts()) : $my_query->the_post();
//content you want to show
endwhile;
wp_reset_postdata();
}
} else{
$my_query2 = new WP_Query( array(
'post_type' => 'produtos',
'posts_per_page' => -1,
'orderby' => 'menu_order',
'order' => 'DESC',
'tax_query' => array(
array(
'taxonomy' => $taxonomyName,
'field' => 'slug',
'terms' => $term->slug,
)
),
));
while ($my_query2->have_posts()) : $my_query2->the_post();
//content you want to show
endwhile;
wp_reset_postdata();
}