I encountered an issue where the plugin does not automatically display anything in categories and tags. If you use the %%category_description%% tag in the settings, it will display all descriptions from the page, which is not ideal. Therefore, let’s create code to truncate it to 170 characters.
One of its useful features is configuring meta descriptions for different sections of your website, such as categories and tags. In this article, we will explore how to use Yoast SEO to configure meta descriptions and excerpts for categories and tags.
1. Go to the plugin settings and configure as shown in the screenshots:
2. Creating a function to truncate text: The first step is to create a function that will be used to truncate text to a specified number of characters. This function removes HTML tags, truncates the text, and returns it at the specified character length.
1 2 3 4 5 6 7 8 9 10 11 12 |
// Function to truncate text to a specified number of characters without cutting off the last word function custom_truncate_text_by_words($text, $length) { $text = strip_tags($text); // Remove HTML tags $text = trim($text); if (mb_strlen($text, 'UTF-8') > $length) { $text = mb_substr($text, 0, $length, 'UTF-8'); $text = preg_replace('/\s+?(\S+)?$/', '', $text); // Remove the last word if it's cut off } return $text; } |
3. Configuring meta descriptions for categories: Next, we create a function that will modify the meta description for categories and product categories in Yoast SEO. We use a check to ensure that we are on a category or product category page.
1 2 3 4 5 6 7 8 9 10 11 12 |
// Filter to change the category meta description for the meta description tag with word truncation function custom_category_meta_description($description) { if (is_category() || is_tax('product_cat')) { // Check if it's a category or product category page $description = category_description(); // Get the category description if ($description) { $description = custom_truncate_text_by_words($description, 170); // Truncate to 170 characters without cutting off the last word } } return $description; } add_filter('wpseo_metadesc', 'custom_category_meta_description'); |
4. Configuring meta descriptions for tags: Similarly, we create a function to change the meta description of tags in Yoast SEO. We check if the current page is a tag page.
1 2 3 4 5 6 7 8 9 10 11 12 |
// Filter to change tag meta description for the meta description tag with word truncation function custom_tag_meta_description($description) { if (is_tag()) { // Check if it's a tag page $tag_description = tag_description(); // Get the tag description if ($tag_description) { $description = custom_truncate_text_by_words($tag_description, 170); // Truncate to 170 characters without cutting off the last word } } return $description; } add_filter('wpseo_metadesc', 'custom_tag_meta_description'); |
Now that you have applied this code, the Yoast SEO plugin will use the configured meta descriptions and truncated excerpts for categories and tags on your WordPress website. This will help improve the SEO optimization of your content and make your site more informative for search engines and visitors. Don’t forget to regularly update the meta descriptions to keep them relevant to your content.
Leave a Reply