• Resolved lukechemistry

    (@lukechemistry)


    Hello! Thanks for the amazing plugin. I’m having no luck with getting alt text to appear on my images within my blocklab blocks while using the image field. I need the alt text to be pulled from the image’s alt text set within the WordPress media library. The docs make no mention of the alt text for the image field and we’re very concerned about accessibility. Is this possible with blocklab?

    Thanks for any help!

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Ryan Kienstra

    (@ryankienstra)

    Hi @lukechemistry,
    Thanks a lot for using Block Lab, and glad to hear you’re enjoying it.

    Sure, there’s a way to get the alt text.

    For the Image field, block_value( 'your-field-name' ) will get the image (attachment) ID.

    From that, you could get the alt text directly like:

    get_post_meta( block_value( 'your-field-name' ), '_wp_attachment_image_alt', true );

    …or this will get the entire image, with the alt:

    wp_get_attachment_image( block_value( 'your-field-name' ) )

    • This reply was modified 5 years, 2 months ago by Ryan Kienstra.
    Thread Starter lukechemistry

    (@lukechemistry)

    Thanks for the prompt reply!

    I’m a little confused on how I could use wp_get_attachment_image( block_value( 'your-field-name' ) ) with an img element.

    <img src="<?php wp_get_attachment_image( block_sub_field( 'image' ) ); ?>"> is pulling in only the img src. I need to be able to call the image and pull in the src and alt text within an img element.

    I also tried this <img src="<?php block_sub_field( 'image' ); ?>" alt="<?php get_post_meta( block_sub_field( 'image' ), '_wp_attachment_image_alt', true ); ?>"> but its pulling in the URL instead of the alt text.

    Thanks for any help!!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Alt text for images’ is closed to new replies.