Are you a GenerateCustomer?

Do you have an active GP Premium or GenerateBlocks Pro license key?

Create a GenerateSupport account for GeneratePress and GenerateBlocks support in one dedicated place.

Create an account
Already have a GenerateSupport account? Login

Just browsing?

Feel free to browse the forums. Support for our free versions is provided on WordPress.org (GeneratePress, GenerateBlocks).

Want to become a premium user? Learn more below.

Limit characters on next/prev post titles

  • 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.

  • 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.

  • You’re welcome.

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.