-
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
-
Fernando
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?
-
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 ย ๐
- You must be logged in to reply to this topic.