If you want to get current product category name from the product in WooCommerce, you can use the $wp_query
object like you would for any other WordPress post. In this tutorial, I will demonstrate how to get current product category from the product.
WooCommerce Get Product Category
Suppose we have this product shown in the image below and we want to know the category name of this product.
As you can see from the image above, we already know the category is ‘Cool products’.
We need to get this current product category in the code as we would get in the ordinary WordPress post using this code:
$wp_query->get_queried_object()->term_id;
WP Function Get the Terms
You can also use get_the_terms() which is a WordPress native function that retrieves the terms of a taxonomy that is associated with a specific post.
The general expression of this function is as follows:
get_the_terms( int|WP_Post $post, string $taxonomy )
The following are the parameter of this function and their respective descriptions:
Parameter | Description |
$post |
This is the post ID or the object that we will use to access the specific product post so that we get the respective category for the product. This can be an integer the POST ID. |
$taxonomy |
This is the taxonomy we want to retrieve using this function. This can either be a category or tag in a WooCommerce product. These are the examples for category and tags;
|
Get Current Product Category Name in WooCommerce
The first step is declaring the global post object then use the function above to get current product category.
To illustrate I will add the action hook to the head and add the code to the callback function to illustrate by displaying the category in the head. Add this code to the functions.php
add_action('wp_head', 'get_current_product_category'); function get_current_product_category(){ global $post; $terms = get_the_terms( $post->ID, 'product_cat' ); $nterms = get_the_terms( $post->ID, 'product_tag' ); foreach ($terms as $term ) { $product_cat_id = $term->term_id; $product_cat_name = $term->name; break; } echo $product_cat_name; }
When you add this code you should see the display of the product category as shown on the image below:
You can verify with the other products on your site to see if the current product category is displayed as shown again in another product like below:
Conclusion
In this post, we have highlighted how you can use the get_the_terms() WordPress function to get current product category in WooCommerce. This is the simplest way you can use to get the product category and display it in your WordPress theme or plugin functions.
Joe is an experienced full-stack web developer with a decade of industry experience in the LAMP & MERN stacks, WordPress, WooCommerce, and JavaScript – (diverse portfolio). He has a passion for creating elegant and user-friendly solutions and thrives in collaborative environments. In his spare time, he enjoys exploring new tech trends, tinkering with new tools, and contributing to open-source projects. You can hire me here for your next project.
Similar Articles
- How to Create Custom Widget Area in WordPress Step by Step
- How to Remove Gravatar Storefront Blog WooCommerce
- How Add Text Before the Price in WooCommerce » Add Text Before Price
- WooCommerce Logout without Confirmation : How to Remove “Are you sure you want to log out?”
- Change Proceed To Checkout Text In WooCommerce
- How to Hide Weight of Product in WooCommerce
- WooCommerce Storefront Theme Review
- How to Hide Price When Out of Stock In WooCommerce
- How to Check if User is Logged In in WordPress
- How to Add WooCommerce Storefront Hero Image
- How to Move Primary Menu Storefront WooCommerce
- How to Fix Uncaught Type Error: wp.template is not a function
- How to Create Shortcode for Plugin in WordPress
- How to Simplify Checkout Free Items Storefront Theme
- How to Create One Product WooCommerce Storefront Store
- WooCommerce Storefront Show all Categories on Homepage
- How to Remove Related Products WooCommerce Storefront Theme
- How to Setup WooCommerce Storefront Blog
- 5+ Useful Ways to Limit WordPress Excerpt Length Like a Pro
Comments are closed.