0

parent1 - child1 - grandchild1

I need to show child terms of any current term (only direct child) for each term I visit. Works only for the parent (show child1) but not for the child (doesn't show granchild1)

How I can fix this issue? Thank's

$queried_object = get_queried_object();
$cpt = get_post_type( get_the_ID() );
$taxonomy = $queried_object->taxonomy;
$term_id = $queried_object->term_id;

echo 'cpt - '.$cpt;
echo '<br>tax - '.$taxonomy;
echo '<br>term id - '.$term_id;

$customPostTaxonomies = get_object_taxonomies($cpt);
if ( $customPostTaxonomies ) {

     foreach($customPostTaxonomies as $tax) {
         $args = array(
              'orderby' => 'name',
              'hierarchical' => 0,
              'taxonomy' => $tax,
              'field' => 'id',
              'parent'=> $term_id,
              'title_li' => ''
              );
         wp_list_categories( $args );
     }

UPDATE:

Structure:
cpt = corsi
tax = argomenti
tax_term = parent1
tax_term_child = child1
tax_term_child_granchild = grandchild1
2
  • You're trying to show multiple taxonomies using the term from one taxonomy - can you confirm you just want to show all immediate child terms of the current taxonomy term? Commented Nov 18, 2015 at 16:34
  • Thank's for reply. I've updated the request. This fuction have to be on taxonomy.php and work for all taxonomy page. And YES, I want to show all immediate child terms of the current taxonomy term. Thank's
    – Simo
    Commented Nov 18, 2015 at 16:40

1 Answer 1

1

Since this is for a taxonomy archive, you can just use get_queried_object() and the parent and depth argument in wp_list_categories():

$term = get_queried_object();
$list = wp_list_categories( array(
    'orderby'  => 'name',
    'taxonomy' => $term->taxonomy,
    'parent'   => $term->term_id,
    'title_li' => '',
    'depth'    => 1,
    'echo'     => false,
));

if ( $list ) {
    echo "<ul>$list</ul>";
} else {
    // Nothing, do something else!
}

Note that wp_list_categories() does not output/return a containing <ul />, so you need to do so yourself (as demonstrated).

6
  • Thank's for help but don't work. When i'm on the child1 page it show me "no category" and not "grandchild1"
    – Simo
    Commented Nov 18, 2015 at 17:22
  • Works for me - I had three categories "Parent", "Child", "Grandchild", have made sure there is at least one post attached to each. Go to "Parent" archive, see "Child". Go to "Child" archive, see "Grandchild". Go to "Grandchild" archive, see nothing (as expected, term has no children). Commented Nov 18, 2015 at 17:29
  • I can confirm the your solution works! Thank's for your time. ps. There isn't a way to show the term also if is empty ?? Thank's for your time.
    – Simo
    Commented Nov 18, 2015 at 20:12
  • You can add an else if there are no terms to show, see revision. Commented Nov 18, 2015 at 21:12
  • if empy, with else [ echo 'something'; }, it show me "no category" and not the echo
    – Simo
    Commented Nov 18, 2015 at 21:17

Your Answer

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

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