Website Building » WooCommerce » How Do I Get Current Category Name in WooCommerce?

How Do I Get Current Category Name in WooCommerce?

Last updated on December 21, 2022 @ 6:23 am

If you’re running a WooCommerce store, there are times when you might need to programmatically get the current category name. For example, you might want to display a different message or image on each category page. In this article, we’ll show you how to get the current category name in WooCommerce.

The easiest way to get the current category name in WooCommerce is by using the get_queried_object() function. This function will return an object containing various information about the current page. You can use this function by adding it to the “Theme Functions (functions.php)“.

Insert the code to functions php WordPress

To access “Theme Functions“, go to your WordPress dashboard, and go to “Appearance” > “Theme Editor”.

Click on Theme Editor Under Appearance of WordPress

On the right-hand side, you will see “Theme Functions” under “Theme Files”; click on “Theme Functions”.

Click on Theme Functions under Theme Files WordPress

We can use this function to get the current category name like this:

$current_cat = get_queried_object();
echo $current_cat->name;

If you want to get the current category ID, you can use the get_queried_object_id() function. This function will return the ID of the currently loaded object. In our case, this will be the ID of the current category:

PRO TIP: The following code will return the current category name in WooCommerce:

$current_cat = get_queried_object();

echo $current_cat->name;

However, this only works if the user is currently viewing a category page. If the user is viewing a product or other type of page, this code will not work.

$current_cat_id = get_queried_object_id();
echo $current_cat_id;

You can also use these functions to get other information about the current category. For example, if you want to get the slug of the current category, you can use the $current_cat->slug property. Or if you want to get the description of the current category, you can use the $current_cat->description property.

Conclusion: How Do I Get Current Category Name in WooCommerce?

 

In conclusion, If you need to programmatically get the current category name in WooCommerce, you can use the get_queried_object() function. You can use this function to get the current category name like this:
$current_cat = get_queried_object();

echo $current_cat->name;

Madison Geldart

Madison Geldart

Cloud infrastructure engineer and tech mess solver.