Shortcode to show the parent taxonomy in woocommerce
To create a shortcode that displays the parent taxonomy name in WooCommerce, you can use the following code:
// Register the shortcode function display_parent_taxonomy_shortcode() { add_shortcode('parent_taxonomy', 'get_parent_taxonomy'); } add_action('init', 'display_parent_taxonomy_shortcode'); // Callback function for the shortcode function get_parent_taxonomy() { // Check if we are on a product category archive page if (is_product_category()) { global $wp_query; // Get the current category object $category = $wp_query->get_queried_object(); // Get the parent category ID $parent_id = $category->parent; // If the parent ID is greater than 0, get the parent category if ($parent_id > 0) { $parent_category = get_term($parent_id, 'product_cat'); $parent_name = $parent_category->name; // Return the parent category name return $parent_name; } } // If not on a product category archive page or parent category not found, return an empty string return ''; } You can add the above code to your theme’s functions.php file or create a custom plugin for it. This code registers a shortcode called [parent_taxonomy] and defines a callback function called get_parent_taxonomy.
When the shortcode is used, it checks if the current page is a product category archive page. If it is, it retrieves the parent category’s name and returns it. If there is no parent category or if the current page is not a product category archive, it returns an empty string.
To display the parent taxonomy name (“Fruits”) on the “Healthy Fruits” product archive page, you can simply add the [parent_taxonomy] shortcode in your desired location within the WordPress editor or a template file. For example:
<p>The parent taxonomy is: [parent_taxonomy]</p> When this shortcode is used on the “Healthy Fruits” product archive page, it will output:
The parent taxonomy is: Fruits Please note that this code assumes you are using the default “product_cat” taxonomy for product categories in WooCommerce. If you have a custom taxonomy, you’ll need to modify the code accordingly by replacing 'product_cat' with the appropriate taxonomy slug.


