Site logo

Archived topic

Dynamic Data settings not working for headline block

9 replies · Started by Trey on March 23, 2023

Viewing posts 1–10 of 10

I am using GeneratePress Elements Loop Template to display the blogs posts on my blog page. It is using the headline dynamic data to display the post exerpt and "read more" link. However, the dynamic data settings are only being applied to the last post in the loop. The Exerpt length setting and "Read more" link only apply to the last post in the query loop. I would like the dynamic data settings to apply for all posts in the query loop.

Hi Ying,

The solution in the article worked to get the "read more" link to show up. It looks like my custom excerpts were the issue. However, I also need to have the excerpts truncated according to the word count setting in dynamic data. Is there a solution for that?

Thanks

However, I also need to have the excerpts truncated according to the word count setting in dynamic data

Try this method:

1. Add an additional CSS class to the headline block which represents the excerpt, eg. custom-excerpt.
https://wordpress.com/support/wordpress-editor/adding-additional-css-classes-to-blocks/

2. Add the below PHP code, change the 10 of $custom_excerpt, 10, '...' to the word count you want to apply:

add_filter( 'render_block', function( $block_content, $block ) {
	if ( ! empty( $block['attrs']['className'] ) && 'custom-excerpt' ===  $block['attrs']['className'] ) {
		$custom_excerpt = get_the_excerpt();
		$limited_excerpt = wp_trim_words( $custom_excerpt, 10, '...' );
		$block_content = sprintf( '%1$s <a href="%2$s">%3$s</a>',
			$limited_excerpt,
			get_permalink(),
			__( 'Read more', 'generatepress' )
		);
	}
	
	return $block_content;
}, 10, 2 );

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

Hi Ying,

Thanks for the code. I have implemented it and it works, however, the formatting that I used for the excerpt's headline block is not working anymore. Specifically, the font size, line spacing, and top border and border color are not working.

Please let me know if you have a solution

Thanks

Hi Fernando,

Your solution worked to truncate the excerpt text. But now the grid is not stacking properly and the "Read more..." text link after the excerpt is gone. Also, some of the formatting in the sidebar is now not working - some of the text has changed color like its a link, and one of the social icons is a weird shape? Seems like a weird side-effect

Can you try this:

1. Remove the codes from me and Fernando.

2. Disable the dynamic data option completely for the headline block.

3. Add static text content, eg. my-custom-excerpt, screenshot below for your reference:
https://www.screencast.com/t/xY7wHydiN4

4. Add this code, change the 10 of $custom_excerpt, 10, '...' to the word count you want.

add_filter( 'render_block', function( $block_content, $block ) {
    if (  ! empty( $block['attrs']['className'] ) && 'custom-excerpt' ===  $block['attrs']['className'] )  {
     
	$custom_excerpt = get_the_excerpt();
        $limited_excerpt = wp_trim_words( $custom_excerpt, 10, '...' );
		
        $new_content = sprintf( '%1$s <a href="%2$s">%3$s</a>',
        $limited_excerpt,
        get_permalink(),
            __( 'Read more', 'generatepress' )
        );
		
	$block_content= str_replace('my-custom-excerpt',$new_content,$block_content );
    }

    return $block_content;
}, 10, 2 );

Let me know how it goes this time!

Hi Ying,

Your code worked!

Really appreciate you and Fernando's help on this one

Thanks!

You're welcome, Trey!

This archived topic is closed to new replies.