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 Data Attribute

  • Hi there, I’ve created a Content Template and wanted to add a data attribute to a featured image since it doesn’t have a title. I added Custom Attribute Title but the value I am trying to set it to something dynamic, like the post title etc. I’ve tried {{post_title}} in the value field but it doesn’t work. Any suggestions?

  • Hi Aldrin,

    Add add-img-alt to the class list of the GB Image Block.

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

    Then add this snippet:

    add_filter( 'render_block', function( $block_content, $block ) {
        if ( !is_admin() && ! empty( $block['attrs']['className'] ) && strpos( $block['attrs']['className'], 'add-img-alt' ) !== false ) {
    		$myreplace = '<img';
    		$myinsert = '<img alt="' . get_the_title() . '"';
            $block_content = str_replace( $myreplace, $myinsert , $block_content );
        }
    
        return $block_content;
    }, 10, 2 );

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

    You can replace alt in the code with your preferred data attribute.

  • Hi Fernando, thank you very much! Worked nicely.

  • You’re welcome, Aldrin!

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