How to custom hide/show a category in single product page in woocommerce?

David Beroff

Well-known member
Registered
Joined
Jun 14, 2016
Messages
1,498
Points
63
I want to hide or show a category in single product page in woocommerce on my Wordpress site. Is this possible? please guide me.
 

MooseLucifer

Well-known member
Registered
Joined
May 20, 2016
Messages
149
Points
28
Yes, it is possible to hide or show a category on a single product page in WooCommerce. There are a few ways to achieve this, depending on your specific needs.

Here are some steps you can follow to hide or show a category in a single product page in WooCommerce:

Step 1: Determine the category ID

To hide or show a category on a single product page, you first need to determine the category ID of the category you want to modify. You can find the category ID by going to Products > Categories in your WordPress dashboard and hovering over the category you want to modify. The category ID will be displayed in the URL at the bottom of your browser window.

Step 2: Create a function in your functions.php file

Once you have the category ID, you can create a function in your functions.php file to hide or show the category on the single product page. Here's an example of a function that will hide a specific category on the single product page:

Code:
function hide_category_on_single_product( $classes ) {
global $post;
$category_id = 123; // replace with your category ID
if ( has_term( $category_id, 'product_cat', $post->ID ) && is_singular( 'product' ) ) {
$classes[] = 'hidden-category';
}
return $classes;
}
add_filter( 'body_class', 'hide_category_on_single_product' );
In this example, the category ID is set to 123, but you should replace this with the actual category ID you want to modify. The function uses the has_term() function to check if the current product has the specified category, and if so, adds the hidden-category class to the body tag. You can use this class to hide the category using CSS.

Step 3: Add CSS to hide or show the category

Once you've added the function to your functions.php file, you can add CSS to hide or show the category on the single product page. Here's an example of CSS that will hide the category if the hidden-category class is present on the body tag:

Code:
.single-product .product_meta .posted_in .hidden-category {
display: none;
}
In this example, the CSS targets the hidden-category class that was added by the function in Step 2. The display: none; rule hides the category on the single product page.

Alternatively, if you want to show the category instead of hiding it, you can use the following CSS:

Code:
.single-product .product_meta .posted_in .hidden-category {
display: block;
}
This CSS targets the hidden-category class and sets the display property to block, which will show the category on the single product page.

With these steps, you should be able to hide or show a category on a single product page in WooCommerce. If you want to modify the function to hide or show multiple categories, you can use the in_array() function to check if the current product has any of the specified categories.
 

harry_v

Well-known member
Registered
Hosting Provider
Joined
Dec 20, 2017
Messages
110
Points
18
You can hide or show a category in the single product page in WooCommerce on your WordPress site without even coding using a plugin called "Hide Categories and Products for Woocommerce." Here are the steps:
  • Install and activate the "Hide Categories and Products for Woocommerce" plugin.
  • Go to the WordPress dashboard and navigate to WooCommerce -> Settings -> Products -> Hide from Categories.
  • Select the product categories you want to show or hide on the single product page.
  • Save the changes.
  • Depending on your selection, the selected categories will now be either shown or hidden on the single product page.
 

Jain Software

New member
Registered
Joined
Jun 5, 2023
Messages
10
Points
1
function hide_show_category_on_single_product_page() {
// Check if it is a single product page
if (is_product()) {
// Get the current product object
$product = wc_get_product(get_the_ID());

// Get the product categories
$categories = $product->get_category_ids();

// Check if the product belongs to a specific category
if (in_array('category-slug', $categories)) {
// Hide or show the category based on your requirement
echo '<style>.product-category.category-slug { display: none; }</style>';
}
}
}
add_action('wp_head', 'hide_show_category_on_single_product_page');


a. Locate the "functions.php" file in your WordPress theme directory.
b. Open the file and add the above code at the end
c. Save the changes to the "functions.php" file.

note- Replace 'category-slug' with the actual slug of the category you want to hide or show. You can find the category slug by navigating to Products → Categories in your WordPress admin dashboard.
 
Older Threads
Newer Threads

Latest Hosting OffersNew Reviews

Sponsors

Tag Cloud

You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an alternative browser.

Top