Red Year
Red Year

Reputation: 45

Hide sub category titles on single parent sub category archive page WooCommerce

I'm building a WP ecommerce site with multiple categories & sub-categories and need to remove the sub-category titles on just one archive page.

All other category & sub category pages still require the sub category titles.

Many thanks.

Upvotes: 1

Views: 1462

Answers (1)

LoicTheAztec
LoicTheAztec

Reputation: 253784

Update - Nov 2018

The easiest way for that purpose is to make a custom conditional function that you will use in the concerned related template content-product_cat.php where the this subcategory title is hooked.

1) The conditional function

Here is that code:

function is_parent_category( $subcategory_term, $parent_category_term_slug ) {
    $parent_term = get_term( $subcategory_term->parent, $subcategory_term->taxonomy );
    return $parent_term->slug == $parent_category_term_slug ? true : false;
}

This code goes in function.php file of your active child theme (or theme) or also in any plugin file.


2) Overriding via your theme **content-product_cat.php WooCommerce template.**

If not done yet, inside your active child theme (or theme) create a woocommerce folder. Then copy from woocommerce plugin templates folder a file named content-product_cat.php inside the fresh created woocommerce folder in your active child theme (or theme).

The open/edit this copied content-product_cat.php template file.

Replace the following line:

do_action( 'woocommerce_shop_loop_subcategory_title', $category );

By this:

if( ! is_parent_category( $category, 'exterior-paving' ) ) {
    do_action( 'woocommerce_shop_loop_subcategory_title', $category );
}

Save, you are done. Now as you wished, all the brand titles only on the 'paving-brands' archive page will be removed.

This code is tested and fully functional.


ADDITION: Handling Multiple parent category slugs:

To handle multiple parent category slugs use the following instead (from an array of slugs):

function is_parent_category( $subcategory_term, $parent_category_term_slugs ) {
    $parent_term = get_term( $subcategory_term->parent, $subcategory_term->taxonomy );
    return in_array($parent_term->slug, $parent_category_term_slug ) ? true : false;
}

USAGE example:

// Reminder: $category is a WP_Term object
if ( is_parent_category( $category, array( 'clothing', 'posters' ) ) {
    // Do something (matching subcategory)
} else {
    // Do something else (no matching subcategory)
}

Upvotes: 1

Related Questions