1

I am trying to make a snippet for WordPress. It should get a list of all subcategories given the id of a parent product category.

There are three levels of product categories, if level 1 is given the result should be all the subcategories that are in each of the lower levels.

I started doing the code but I didn´t finished it, and not sure where is the issue. On top of that I am using Oxygen builder, and this is why I created a shortcode on CodeSnippet plugin, and added the shortcode on Oxygen.

This is the code I am trying, this part should return the subcategories of a the parent category.

     add_shortcode( 'subcategoriasdecategoria', function () {

?>
<ul class="megamenu_categoria2"> 
<?php
$get_parent_cats = array(
            'parent' => '0' //get top level categories only
        ); 

        $all_categories = get_categories( $get_parent_cats );//get parent categories 

        foreach( $all_categories as $single_category ){
            //for each category, get the ID
            $catID = $single_category->cat_ID;

            echo '<li><a href=" ' . get_category_link( $catID ) . ' ">' . $single_category->name . '</a>'; //category name & link
            $get_children_cats = array(
                'child_of' => $catID //get children of this parent using the catID variable from earlier
            );

            $child_cats = get_categories( $get_children_cats );//get children of parent category
            echo '<ul class="megamenu_categoria2">';
                foreach( $child_cats as $child_cat ){
                    //for each child category, get the ID
                    $childID = $child_cat->cat_ID;

                    //for each child category, give us the link and name
                    echo '<a class="megamenu_categoria2" href=" ' . get_category_link( $childID ) . ' ">' . $child_cat->name . '</a>';

                }
            echo '</ul></li>';
        } //end of categories logic 
    
    return $out;
} ); 

Can anyone help me?

1 Answers1

0

First, you have to get all parent categories, and then based on parent id you can write a recurring function that gives you the child category of given id. check below code.

function get_child_categories( $parent_category_id ){
    $html = '';
    $child_categories = get_categories( array( 'parent' => $parent_category_id, 'hide_empty' => false, 'taxonomy' => 'product_cat' ) );
    if( !empty( $child_categories ) ){
        $html .= '<ul>';
        foreach ( $child_categories as $child_category ) {
            $html .= '<li>'.$child_category->name;
            $html .= get_child_categories( $child_category->term_id );
            $html .= '</li>';
        }
        $html .= '</ul>';
    }
    return $html;
}

function list_categories(){
    $html = '';
    $parent_categories = get_categories( array( 'parent' => 0, 'hide_empty' => false, 'taxonomy' => 'product_cat' ) );
    $html.= '<ul>';
    foreach ( $parent_categories as $parent_category ) {
        $html .= '<li>'.$parent_category->name;
        $html .= get_child_categories( $parent_category->term_id  );
        $html .= '</li>';
    }
    $html.= '</ul>';
    return $html;
}
add_shortcode( 'list_categories', 'list_categories' );

Output

  • A
    • B
    • C
      • D
  • E
    • F
    • G
      • H
Bhautik
  • 11,125
  • 3
  • 16
  • 38
  • Thank you very much @Bhautik! It works fine to get all post categories. But How can make to get all product categories? I tried with 'product_cat' => $taxonomy as an argument but it is not working. – Raul Jauregi Mar 21 '21 at 12:40
  • 1
    You should accept @Bhautik's answer. Yes, product_cat is the correct taxonomy. https://stackoverflow.com/questions/21009516/get-woocommerce-product-categories-from-wordpress – DeFeNdog Mar 21 '21 at 14:51