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.

Limiting Product Title Characters in GP Query Block: Exploring Options

  • I want to limit the number of characters displayed in the product title. I’m currently using the Heading block to show the title. Is there a way to achieve this through block settings, or are there any hooks or filters available that can be applied when using the GP Query block.

    https://postimg.cc/F7SggF0s

  • Hi there,

    The screenshot is selecting the list of terms, is this what you are referring to?

    Or is there a headline to pull the product title?

    Just to make sure you are trying to show a shorter version of the product title, right?

  • Correct, there is a headline block to pull the product title and show the shorter version. In addition, I want this to be filtered by characters rather than words.

  • Hi there,

    A PHP snippet like this may help:

    add_filter( 'generateblocks_dynamic_content_output', function( $content, $attributes ) {
        $class_name = $attributes['className'] ?? '';
    
        if ( 'truncate-text' !== $class_name ) {
            return $content;
        }
    
        $number_of_characters = 10;
        $content = mb_strimwidth( $content, 0, $number_of_characters, '...' );
    
        return $content;
    }, 10, 2 );

    In order for this to work, you need to give the block this CSS class in the “Advanced” panel: truncate-text

    Let me know if it helps or not 🙂

  • Hi Tom!
    Thank you for sharing the snippet.
    This helped me a lot.

  • Glad I could help!

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