-
birdhousedigital
Hi, I’m using {{post_title source:next-post}} in buttons to show the title of the next or previous post. This works well but I’m wondering how I set a max character limit on this as a few of the post titles are too long and it doesn’t look great?
Many thanks.
-
Alvind
Hi there,
You can try this snippet:
add_filter('generateblocks_dynamic_tag_output', function ($output, $options) { if (isset($options['trim-title'])) { if (!empty($output)) { $num_words = !empty($options['trim-title']) && is_numeric($options['trim-title']) ? intval($options['trim-title']) : 3; return wp_trim_words($output, $num_words, '...'); } } return $output; }, 10, 2);Then, in your dynamic tag, use it like this:
{{post_title source:next-post|trim-title}}
By default, it will trim the title to 3 words.If you want to specify a custom word count (e.g., 4 words), use:
{{post_title source:next-post|trim-title:4}} -
birdhousedigital
Works perfectly.
Thank you. -
Alvind
You’re welcome.
Viewing 4 posts - 1 through 4 (of 4 total)
- You must be logged in to reply to this topic.