Home › Forums › WoodMart support forum › How do you add a meta tag to the head? › Reply To: How do you add a meta tag to the head?
serkanisim
add those codes to functions.php
###For both WooCommerce and WordPress you will use:
add_action( ‘wp_head’, ‘wcs_add_meta_keywords’ , 2 );
function wcs_add_meta_keywords() {
// For WordPress single posts with categories and tags
if ( is_single() && ! is_product() ) {
$cats = (array) wp_get_post_terms( get_the_id(), ‘category’, array(‘fields’ => ‘names’) );
$tags = (array) wp_get_post_terms( get_the_id(), ‘post_tag’, array(‘fields’ => ‘names’) );
}
// For WooCommerce single product (product categories and product tags)
elseif ( is_product() ) {
$cats = (array) wp_get_post_terms( get_the_id(), ‘product_cat’, array(‘fields’ => ‘names’) );
$tags = (array) wp_get_post_terms( get_the_id(), ‘product_tag’, array(‘fields’ => ‘names’) );
}
if ( ! empty( $cats ) || ! empty( $tags ) ){
echo ‘<meta name=”keywords” content=”‘ . implode( ‘, ‘, array_merge( $cats, $tags ) ) . ‘” />’ . “\n”;
}
}
####For WooCommerce only use:
add_action( ‘wp_head’, ‘wcs_add_meta_keywords’, 2);
function wcs_add_meta_keywords() {
if ( is_product() ) {
$product_cats = (array) wp_get_post_terms( get_the_id(), ‘product_cat’, array(‘fields’ => ‘names’) );
$product_tags = (array) wp_get_post_terms( get_the_id(), ‘product_tag’, array(‘fields’ => ‘names’) );
}
if ( ! empty( $product_cats ) || ! empty( $product_tags ) ){
echo ‘<meta name=”keywords” content=”‘ . implode( ‘, ‘, array_merge( $product_cats, $product_tags ) ) . ‘” />’ . “\n”;
}
}