0

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();
}
  • Hi. I am sorry I did not reply earlier. Was busy with work. I think you are on the right track. However, I would like to ask about the URL you are using to display your categories and sub categories? This will help. – Greeso Aug 12 '17 at 3:15
  • We changed the way things work, and now, in the admin, we only have the custom taxonomy, a categorie, and sub.cats, that are direct children. But when I click on a child categorie, it doesnt show any posts. Here is the link: testes.luzepaz.org/o2a/produtos – Marcelo Henriques Cortez Aug 12 '17 at 7:48
  • I edited the code to show what I have so far. – Marcelo Henriques Cortez Aug 12 '17 at 7:52
  • Ok, I solved the problem. Don't think those links helped me directly, but when I was tweaking the code, found some things that could be a problem and changed the code. Gonna update with the solution. – Marcelo Henriques Cortez Aug 13 '17 at 9:11
1

I think you can easily achieve that if you understand WordPress template hierarchy.

Looking at the template hierarchy diagram, and assuming you have categories and sub-categories, then you can achieve what you want in multiple ways.

Suggestion 1

  1. Use category.php to display the posts of any category, including sub-categories.

  2. also in category.php add a condition at the top to detect if it is a "parent" category. If so, then display the sub-categories of that parent category.

Suggestion 2

  1. Use category.php to display the posts of any category, including sub-categories.

  2. Use category-$slug.php for the parent category, and use it to display the sub categories for that parent.

I think suggestion 2 would be OK if you have one, or two parent categories. Because every time you add a new parent category, you have to create a new category-$slug.php file for it. So if you have many parent categories to be created, then use suggestion 1.

Also, you can use the same solution for custom taxonomies as well, but use the hierarchy diagram to understand taxonomy files (i.e., use taxonomy.php, taxonomy-$taxonomy.php and taxonomy-$taxonomy-$term.php depending on your needs).

  • But my problem is with the loops, because I have to get the current custom tax, loop to check if it has sub-cats and if it does, show them. And show every post in them. – Marcelo Henriques Cortez Aug 7 '17 at 18:56
  • I do not understand. What do you mean? – Greeso Aug 7 '17 at 21:18
  • You told me to use the hyerarchy and etc. But I need to make a loop to get the content the way I need, it doesn't make a diference if is a regular page or a 'taxonomy-slug' one. I need to make a loop to get the current custom tax, check the sub-cats inside of it, show their names with the posts inside of them. – Marcelo Henriques Cortez Aug 7 '17 at 21:43
  • In this case, use two loops. Your regular loop for the regular page, and then a custom loop for every sub-category you like to create. And combine them within each other anyway you wish to display the content you need. – Greeso Aug 7 '17 at 21:49
  • But I need a code that gets all sub-categories from that custom taxonomy automatically, because they will change in the future (have more, delete some, etc). And I have no idea how to make that loop. I know that is possible, just haven't found any code that worked for me. – Marcelo Henriques Cortez Aug 7 '17 at 21:57
0

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();
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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