Custom Taxonomy – Modify Function to Get Child Category

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

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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;


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x