-
aldrin151
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?
-
Fernando
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. -
aldrin151
Hi Fernando, thank you very much! Worked nicely.
-
Fernando
You’re welcome, Aldrin!
Viewing 4 posts - 1 through 4 (of 4 total)
- You must be logged in to reply to this topic.