0

I was asked to make a dynamic menu with sub-items. This is the idea:

$categories = array{
‘cars’,
‘boats’,
‘airplanes’
};
Home About us Categories
Cars
Boats
Airplanes

On the Categories session, I’ll place an array dynamically populated with all categories. And… The most important part… Every time a new category is added by the user, a new sub-item must be added automatically.

Is it possible to do it?

1 Answer 1

0

yes, of course it is possible to do. Here is the code to get all the categories on a page load, this code is prepared to go until the 3rd category generation.

$cats = get_categories( array('taxonomy' => 'product_cat', 'hide_empty' => true) );
foreach ($cats as $key => $cat):
    if ($cat->parent == 0):
        echo $cat->name;
        foreach ($cats as $key => $cat2):
            if ($cat2->parent == $cat->term_id):
                echo $cat2->name;
                foreach ($cats as $key => $cat3):
                    if ($cat3->parent == $cat2->term_id):
                        echo $cat3->name;
                    endif;
                endforeach;
            endif;
        endforeach;
    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.