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.

Dynamic block element Title lenght

  • Hello,

    I am wondering if you could please help me sort out an issue. I created a Dynamic block element to display the blog posts in a grid. The problem I am having is that one of the items (first column last row) has a very long title and it stretches the layout of the box way past the others. I am wondering if there is a way to limit the length of the tiles. Here is the link to the page in question:

    https://a4b.11c.myftpupload.com/category/pe-primers-bookshelf/

    Thank you in advance

  • Hi Sofia,

    Here are a few steps:

    1. Add cu-limit-text to the class list of the Headline Block.

    Adding Custom Classes: https://wordpress.com/support/wordpress-editor/adding-additional-css-classes-to-blocks/

    2. Add this snippet:

    add_filter('generateblocks_dynamic_content_output', function($content, $attributes, $block){
    	if ( ! is_admin() && ! empty( $attributes['className'] ) && strpos( $attributes['className'], 'cu-limit-text' ) !== false ) {
    		$words = preg_split("/\s+/", $content);
    		if (count($words) > 8) {
    			$words = array_slice($words, 0, 8);
    			$content = implode(' ', $words);
    		}
    	}
    	return $content;
    }, 10, 3);

    Adding PHP: https://docs.generatepress.com/article/adding-php/#code-snippets

    Let us know how it goes.

  • Hi there, thank you for your prompt reply, I followed the steps but I am afraid it did not work ๐Ÿ™

  • Can you take a screenshot of the steps you’ve done?

  • Images

    I am not sure how to upload images here. I hope this works:
    https://photos.app.goo.gl/gmxMd8YaDRv6pqo86

  • Your settings are correct, however, this snippet doesn’t work.

    Can you try this snippet to trim the post title itself for the archive and blog page directly?

    You just need to change the 20 to fit your needs ๐Ÿ™‚

    function trim_archive_post_titles($title) {
        // Check if we are on a blog or an archive page (e.g., category, tag, author, date archive)
        if (is_archive() || is_home()) {
            // Define the maximum number of words to display
            $max_words = 20; // Display up to 20 words
    
            // Explode the title into an array of words
            $words = explode(' ', $title);
    
            // If the title has more words than the limit, truncate it to max words
            if (count($words) > $max_words) {
                $title = implode(' ', array_slice($words, 0, $max_words)) . '...'; // Add ellipsis
            }
        }
    
        return $title;
    }
    add_filter('the_title', 'trim_archive_post_titles');
  • That worked! Thank you very much!

  • You are welcome ย  ๐Ÿ™‚

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