Article updated in November 2026.
When you use Yoast SEO, category and tag pages do not always get a meta description automatically. If you put the %%category_description%% variable into the search appearance template, Yoast outputs the whole term description. On long descriptions that is far too much for a snippet. This article shows how to cut the description to about 170 characters with a small code snippet.
Yoast SEO lets you set meta descriptions for different parts of the site, including categories and tags. Below, we set up the templates and then use the wpseo_metadesc filter to shorten the text.
1. Configure the templates in Yoast SEO
Open the plugin settings (Yoast SEO → Settings → Categories and tags, in older versions Search Appearance → Taxonomies). Set the meta description template for categories and tags as shown in the screenshots. The template should contain the category or tag description variable.
Yoast also has more general variables for term descriptions. Check the variable list in the template field of your version, because names can differ slightly between releases.
2. Create a function to truncate text
First we need a helper that shortens text to a given number of characters. It removes HTML tags and shortcodes, normalizes whitespace, cuts the text and then removes the last word if it was cut in half. The result never ends in the middle of a word.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
// Truncate text to a specified number of characters without cutting off the last word function custom_truncate_text_by_words($text, $length = 170) { $text = wp_strip_all_tags(strip_shortcodes($text)); // Remove shortcodes and HTML tags $text = html_entity_decode($text, ENT_QUOTES, 'UTF-8'); $text = trim(preg_replace('/s+/u', ' ', $text)); // Normalize whitespace if (mb_strlen($text, 'UTF-8') <= $length) { return $text; } $cut = mb_substr($text, 0, $length, 'UTF-8'); $next = mb_substr($text, $length, 1, 'UTF-8'); // If the cut happened inside a word, drop that partial word if (!preg_match('/s/u', $next)) { $cut = preg_replace('/s+S*$/u', '', $cut); } return rtrim($cut, " ,.;:-") . '…'; } |
The original version of this helper used strip_tags() and a regular expression only. The version above also handles shortcodes, HTML entities and multiple spaces, which are common in term descriptions.
3. Meta descriptions for categories
Next we hook into wpseo_metadesc and replace the description on category pages and on WooCommerce product category pages. The condition makes sure the code runs only on those archives.
|
1 2 3 4 5 6 7 8 9 10 11 12 |
// Filter to change the category meta description with word truncation function custom_category_meta_description($description) { if (is_category() || is_tax('product_cat')) { // Category or product category page $term_description = category_description(); // Get the category description if ($term_description) { $description = custom_truncate_text_by_words($term_description, 170); } } return $description; } add_filter('wpseo_metadesc', 'custom_category_meta_description'); |
4. Meta descriptions for tags
Tags work the same way. We check that the current page is a tag archive and shorten the tag description.
|
1 2 3 4 5 6 7 8 9 10 11 12 |
// Filter to change the tag meta description with word truncation function custom_tag_meta_description($description) { if (is_tag()) { // Tag page $tag_description = tag_description(); // Get the tag description if ($tag_description) { $description = custom_truncate_text_by_words($tag_description, 170); } } return $description; } add_filter('wpseo_metadesc', 'custom_tag_meta_description'); |
Add all the code to the functions.php file of your child theme, or to a code snippets plugin. Do not edit a parent theme directly, because updates will overwrite your changes.
Result
After you apply the code, Yoast SEO outputs a meta description for categories and tags automatically, and the text is shortened at a word boundary.



Leave a Reply