0

I am using these two functions which result in getting the top parent category of a custom taxonomy. These have been tested and work as expected.

However, now I am looking for a way to output the child category instead of the parent.

function get_term_top_most_parent( $term, $taxonomy ) {
    // Start from the current term
    $parent  = get_term( $term, $taxonomy );
    // Climb up the hierarchy until we reach a term with parent = '0'
    while ( $parent->parent != '0' ) {
        $term_id = $parent->parent;
        $parent  = get_term( $term_id, $taxonomy);
    }
    return $parent;
}

function get_top_parents( $taxonomy ) {
    // get terms for current post
    $terms = wp_get_object_terms( get_the_ID(), $taxonomy );
    $top_parent_terms = array();

    foreach ( $terms as $term ) {
        //get top level parent
        $top_parent = get_term_top_most_parent( $term, $taxonomy );
        //check if you have it in your array to only add it once
        if ( !in_array( $top_parent, $top_parent_terms ) ) {
            $top_parent_terms[] = $top_parent;
        }
    }

    // build output (the HTML is up to you)
    foreach ( $top_parent_terms as $term ) {
          //Add every term
          $output = $term->slug;
    }

    return $output;
}

1 Answer 1

0

if it is for product categories, this function only needs the category id to know the childrens:

function cat_childs( $term_id = 0) {
  $children = get_terms( array(
        'child_of'      => $term_id,
        'taxonomy'      => 'product_cat',
        'hide_empty'    => true,
        'fields'        => 'ids',
  ));
return ( $children );
}

can be called like this:

cat_childs(1234);

because it can return more than one that we can check first using

$a = cat_childs(1234);
echo '<pre>';
print_r($a);

if it is the case, we just need to do a foreach loop to get them all.

This is my code to make a menu with them all until the 3rd generation that uses the cat_childs() function to check if there is a need to the next loop.

$cat_args = array( 'orderby' => 'name', 'order' => 'asc', 'hide_empty' => true);
$cats = get_terms( 'product_cat', $cat_args );
foreach ($cats as $key => $cat):
  if ($cat->parent == 0): ?>
    <p><a href="<?php echo get_term_link($cat) ?>"  style="color:red;"><?php echo $cat->name; ?></a></p><?php
    if (cat_childs($cat->term_id)):
      foreach ($cats as $key => $cat2):
        if ($cat2->parent == $cat->term_id): ?>
          <p><a href="<?php echo get_term_link($cat2) ?>" style="margin-left:20px;  color:blue;"><?php echo $cat2->name; ?></a></p><?php
          if (cat_childs($cat2->term_id)):
            foreach ($cats as $key => $cat3):
              if ($cat3->parent == $cat2->term_id): ?>
                <p><a href="<?php echo get_term_link($cat3) ?>" style="margin-left:40px; color:green;"><?php echo $cat3->name; ?></a></p><?php
              endif;
            endforeach;
          endif;
        endif;
      endforeach;
    endif;
  endif;
endforeach;

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.