Skip to content
Advertisement

Hot to add subcategories under category element in woocommerce flatsome theme WordPress

I’m trying to set up e-commerce website. Ran into a problem with Categories element. I want the element to represent not only categories, but also subcategories below itself.

I also cannot edit page without UX builder, I think that’s because Flatsome theme elements.

I want something like that :

Category_1 Category_2

Subcat1.1 ___Subcat2.1

Subcat1.2 ___Subcat2.2

Like that:

My example in image

But, currently I can put categories only. Cannot insert subcategories below. Cannot change Category element.

Currently tried few examples here on stack, using php scripts, but failed.

I ask you for your help, because I don’t now where to seek for info I need.

Is it even possible with given circumstances? Or I need to create whole Page without Flatsome, and then add custom element?

Advertisement

Answer

Here is the code to get parent and child category.

$parent_cat_arg = array('hide_empty' => false, 'parent' => 0 );
$parent_categories = get_terms('category',$parent_cat_arg);

foreach ($parent_categories as $category) {

    echo '<h2>'.$category->name.'</h2>'; //Parent Category

    $child_arg = array( 'hide_empty' => false, 'parent' => $category->term_id );
    $child_cat = get_terms( 'category', $child_arg );

    echo '<ul>';
        foreach( $child_cat as $child_term ) {
            echo '<li>'.$child_term->name . '</li>'; //Child Category
        }
    echo '</ul>';
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement