• tekstraket

    (@tekstraket)


    Hi there!

    At my homepage I have three ‘featured blocks’. Those blocks have an image above. But those are missing the alt attribute.

    My homepage has it’s own php-file: page_home.php. The blocks are defenied by this code:

    <div class=”span4″>
    <?php
    $thumbnail = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), ‘your_thumb_handle’ );
    ?>
    ” />
    <div class=”homeblokcontent”><?php the_content(); ?></div>
    </div><!–span4–>

    Has anyone an idea how I can add the alt attribute? I am searching for hours, but online I only find the situation for thumbnail from blog posts. And that is not the case.

    If I change the line with the img src to this:

    ” />

    I get an unexpected syntax error due to ‘=’ and the page doesn’t load anymore.

    Hope you can help me!
    Regards, Jessie

    The page I need help with: [log in to see the link]

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    You didn’t include the code that does something with the $thumbnail value. No matter. It’s maybe something like this:
    <img src="<?php echo $thumbnail; ?>" />

    Hopefully there is more to it than that, like srcset and sizes attributes, but apparently any use of an alt attribute is missing. The point is that wp_get_attachment_image_src() does exactly that, it returns only the src attribute value, nothing else needed for a proper img tag is returned.

    It’s possible to get just the alt attribute value for an image and add it to the img tag code, but there’s usually little need to get into this level of detail. There is a WP function that does all of this for us, it generates the entire HTML img tag all at once — src, alt, srcset, sizes attributes, everything. You can even have it include your class and id attributes, as well as any other acceptable attributes. I would rework the template to use this awesome function and not mess with the nitty gritty img attribute details myself.

    This awesome function is wp_get_attachment_image()

Viewing 1 replies (of 1 total)
  • The topic ‘alt attribute is missing: wp_get_attachment_image_src’ is closed to new replies.