I want to display de category description in a meta tag but my code does not return the category description but the description of the first product of the store page
<head>
<meta name="description" content="<?php
if( is_product_category() ){
$catID = get_the_category();
echo category_description( $catID[0] );
} else
$description = get_the_content();
if ( $description) {
echo wp_strip_all_tags($description);
}
?>"/>
2
Answers
There are some mistakes in your code:
get_queried_object_id()
,is_singular()
conditional function to target single post and product pages,Try to use the following instead:
It should work.
If you want to target only single product pages (but not blog posts too), replace the line:
with:
get_queried_object_id()
will retrieves the current queried id.$category->term_id
Accesses the ID of the current category, which is then used to fetch the category description.get_the_excerpt()
function retrieves the excerpt for the current post. If the post does not have a manual excerpt, it generates one from the post content.Function for truncate the description to 160