1

I know this has been asked and answered before but the solutions are not working for my so I was wondering if something has changed or if I am doing something wrong. Want to display my Categories, Child Cats, Grand Child-Cats. This is my code:

function list_areas () {
$provincearg = array(
                            'taxonomy' => 'area',
                            'hide_empty' => false,
                            'parent'=> 0,
                        );
    $provinces = get_terms ($provincearg);
        foreach ($provinces as $province) {
            echo '<h2>' .$province -> name. '</h2>';

            $municipalarg = array (
                                    'taxonomy' => 'area',
                                    'hide_empty' => false,
                                    'parent'=> $province -> term_id,
                                    );
            $municipalities = get_terms ($municipalarg);

            echo '<ul>';
                foreach ($municipalities as $municipality) {
                    echo '<li>' .$municipality -> name. '</li>';
                }
            echo '<ul>';
        }
}

It outputs the Category but not the sub categories. I even went as far as puting in the 'parent' id, when I do this nothing gets displayed.

Any help will be appreciated.

1
  • An update: This code works for my categories but not my custom categories. I didn't ad the custom taxonomy via the admin panel. I loaded it via a CSV file. I am not sure if somehow that affects the wordpress fucntions somehow.
    – Ragnaboy
    Feb 26, 2018 at 13:33

2 Answers 2

0

You can achieve categories and sub-categories this way.

<?php 

$list_categories = get_categories( $args );
echo "<div class='container'>";
echo "<div class='productsubmenu'>";
echo "<ul>";

foreach ($list_categories as $cat)
{           

if($cat->category_parent == 0)
{
$category_id = $cat->term_id;

echo '<li class="parent '.$my.' "><a href="'.get_term_link( $cat ) . '">'.$cat->name.'</a>';


$args2 = array(
'taxonomy' => $taxonomy,
'child_of' => 0,
'parent' => $category_id,
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title,
'hide_empty' => $empty

);
$args = array(
'hierarchical' => 1,
'show_option_none' => '',
'hide_empty' => 1,
'parent' => $parent_cat_ID,
'taxonomy' => 'product_cat'
);
$subcats = get_categories($args2);
echo '<ul class="wooc_sclist">';

foreach ($subcats as $sc) { 
$link = get_term_link( $sc->slug, $sc->taxonomy );
echo '<li class="" ><a href="'. $link .'">'.$sc->name.'</a></li>';
}


}
echo '</ul>';
echo "</li>";
}

?>
0

So after strugggling and rewriting the code. I have finally solved this.

The issue was not with the code, the issue was when I registered my custom taxonomy, I spelled hierarchical incorrectly which is why it wouldn't get the child terms.

Your Answer

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

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