Hello @traquila,
Thank you for contacting Rank Math support.
You can use this filter to register a new variable to get a long description of your product category, removing all the HTML tags and limiting the words to 150:
add_action( 'rank_math/vars/register_extra_replacements', function() {
rank_math_register_var_replacement(
'woo_full_desc',
[
'name' => esc_html__( 'Woo Full Desc.', 'rank-math' ),
'description' => esc_html__( 'Woo Full Description…', 'rank-math' ),
'variable' => 'woo_full_desc',
'example' => woo_full_desc_callback(),
],
'woo_full_desc_callback'
);
} );
function woo_full_desc_callback() {
global $post;
if ( empty( $post ) ) {
return 'Product Description';
}
return mb_strimwidth( wp_strip_all_tags( $post->post_content ), 150, '…', 'UTF-8' );
}
Once done, you can use the %woo_full_desc% variable in the SEO description field.
Here’s how you can add a filter/hook to your WordPress site:
https://rankmath.com/kb/wordpress-hooks-actions-filters/
Hope that helps.
@rankmathsupport
You are truly amazing. You’ve given me another reason to recommend the Rankmath plugin to my clients.
The 150 character limit gave me an error, but I solved it with such a code correction.
function woo_full_desc_callback() {
global $post;
if ( empty( $post ) ) {
return 'Product Description';
}
$full_description = get_the_content( null, false, $post );
$short_description = mb_substr( $full_description, 0, 150, 'UTF-8' ) . '…';
return $short_description;
}
How can I print the description of the relevant category here?
This shows the description of the first product in that category
-
This reply was modified 1 year, 3 months ago by
traquila.
-
This reply was modified 1 year, 3 months ago by
traquila.
Hello @traquila,
The filter we shared before was to create a variable to get the product description. Please replace it with the following filter:
add_action('rank_math/vars/register_extra_replacements', function () {
rank_math_register_var_replacement(
'term_desc',
[
'name' => esc_html__('Term description', 'rank-math'),
'description' => esc_html__('Description of current term', 'rank-math'),
'variable' => 'term_desc',
'example' => term_desc_callback(),
],
'term_desc_callback'
);
});
function term_desc_callback()
{
$term = get_queried_object_id();
$desc = term_description($term);
return mb_substr( $desc, 0, 150, 'UTF-8' ) . '…';
}
Once done, you can use the %term_desc% variable to get the correct description.
Hope that helps and please do not hesitate to let us know if you need our assistance with anything else.
@rankmathsupport It really worked thank you. God bless you.
Hi @traquila,
We are super happy that this resolved your issue. If you have any other questions in the future, know that we are here to help you.
If it isn’t too much to ask for – would you mind leaving us a review here?
https://wordpress.org/support/plugin/seo-by-rank-math/reviews/#new-post
https://www.trustpilot.com/evaluate/www.rankmath.com
It only takes a few minutes, but it makes a huge difference.
It would mean so much to us and would go a long way.
Thank you.