Site logo

Archived topic

How to add nofollow to a dynamic data heading in templates?

5 replies · Started by Laurentiu on January 31, 2023

Viewing posts 1–6 of 6

Hi there,

1. Add this PHP Snippet to your site:

add_filter( 'render_block', function( $block_content, $block ) {
    if ( 
        ! empty( $block['attrs']['className'] ) 
        && 'nofollow' === $block['attrs']['className']  
    ) {
        $block_content = str_replace( '<a ', '<a target="_blank" rel="nofollow" ', $block_content );
    }

    return $block_content;
}, 10, 2 );

How to add PHP: https://docs.generatepress.com/article/adding-php/

2. Select the block you have for your dynamic link and in Advanced > Additional CSS classes add: nofollow

Hi there,

Just checked the page, I can see the nofollow class has been added to the H2 headline block correctly, but the PHP code David provided doesn't seem to be working.

How did you add the code?

Working now, my mistake.
Thank you!

No Problem :)

This archived topic is closed to new replies.