I am aware this question is very similar to previous questions however none of the provided answers seem to work for me.

but my question is, how do I divide sub-categories by parent category?

For instance, If I click Balloons on this website (see first image), it then brings me to a new page where the sub categories are divided and the children of the sub category organised underneath it (see image 2 & 3).

This is the code I have, it gives me the desired functionality, however the layout is wrong, I want parent title followed by child images much like the layout in the images I have provided )(I am aware this is due to the hook I am using but I don't know the right one to use) EDIT: I have since changed the hook to 'woocommerce_after_subcategory' but that did not seem to work at all, they still display after subcategory title

// Adding Child Category List to Main Category Display Grid

add_action('woocommerce_after_subcategory_title', 'woocommerce_subcats_from_parentcat_by_ID', 20);

function woocommerce_subcats_from_parentcat_by_ID($category) {
    $parent_category_ID = $category->term_id;
    $args = array(
        'hierarchical' => 1,
        'show_option_none' => '',
        'hide_empty' => 0, // Set to 0 to show empty categories and 1 to hide them
        'parent' => $parent_category_ID,
        'taxonomy' => 'product_cat'
    $subcategories = get_categories($args);
    echo '<ul class="woo_subcategory_list">';
    foreach ($subcategories as $subcategory) {
        $link = get_term_link( $subcategory->slug, $subcategory->taxonomy );
        echo '<li><a href="'. $link .'">'.$subcategory->name.'</a></li>';
    echo '</ul>';

enter image description here

enter image description here

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.