Used to get the category of the current product
All Echo content will be printed in the header of the page.
The following code must be inserted in the function.php contained in your child theme (**).
/* This code shows the Category of a woocommerce product */
add_action('wp_head', 'get_current_product_category');
function get_current_product_category(){
global $post;
$terms = get_the_terms( $post->ID, 'product_cat' );
if($terms) {
foreach ($terms as $term){
$product_cat_id = $term->term_id;
$product_cat_name = $term->name;
break;
}
}
echo $product_cat_name ." " . $product_cat_id ;
}
(**) How to Create a Child Theme
A child theme is a theme that inherits the functionality and styling of another theme, called the parent theme. Child themes are the recommended way of modifying an existing theme.
- Create child theme directory (common approach to name child theme as the parent theme plus -child added on the end.)
- Create style.css
- Create functions.php
For more detailed instructions on child themes you can visit the following links:
https://developer.wordpress.org/themes/advanced-topics/child-themes/
https://blog.hubspot.com/website/wordpress-create-child-theme
Related posts: