• Cedric

    (@cooperanet)


    Hi,

    Is there a way to only retrieve the ALT attribute value of an image linked as thumbnail (with this great new 2.9 feature)?

    Any help would be greatly appreciated!

Viewing 5 replies - 1 through 5 (of 5 total)
  • Frank.Prendergast

    (@frankprendergast)

    Not sure exactly what you mean cooperanet, but I have been trying to figure out two things today:

    1. Why the alt attribute is alt=”” using the_post_thumbnail
    2. How to get *just* the alt attribute of an image being used as a thumbnail

    Unfortunately I don’t have answers to either.

    What I did in the end was get the title of the image and insert that into the alt attribute and also use that text where I wanted to use the alt text.

    $thumb_id = get_post_thumbnail_id( $post->ID );
    $thumb_title = get_the_title( $thumb_id );

    Then you can use $thumb_title wherever you need it, including setting is the alt attribute:

    the_post_thumbnail('main-image', array('alt' => $thumb_title));

    If anyone knows why the alt attribute is showing up as alt=”” or how to actually retrieve the alt and not the title I’d love to hear.

    Not sure where exactly I found the above solution, but some great tips in the comments on this thumbnails post.

    Frank.Prendergast

    (@frankprendergast)

    ps ‘main-image’ is my thumbnail name as defined in functions.php

    esmi

    (@esmi)

    On the basis that a post thumbnail is just that – a primarily decorative image intended to visually enhance the post teaser – a null alt attribute would seem to be the most appropriate.

    Thread Starter Cedric

    (@cooperanet)

    Hello Frank and Esmi,

    Thanks for your response.

    In fact, for one of my client I use the thumbnail feature (which is great) but through a customized function in order to use it with the timthumb script. It’s seems easier to manage for my client.

    Here is the customized function:

    function get_the_image() {
    	$imageFull = wp_get_attachment_image_src ( get_post_thumbnail_id ( $post->ID ), 'large' );
    	echo $imageFull[0];
    }

    And the thumbnail is called like that:
    <img src="<?php bloginfo('template_url'); ?>/timthumb/timthumb.php?src=<?php get_the_image(); ?>&h=200&w=600&zc=1&q=100" alt="" border="0" />

    So, for a better SEO, it would be great to let my client choose the alt attribute through the dedicated field in the image properties than limiting it to the title of the post/page (as Frank suggested it).

    So if someone knows the trick to retrieve the value inserted in the “alt” field of the image panel, it would be great!

    Apparently the problem is with the wp_get_attachment_image and wp_get_attachment_image_src functions which the_post_thumbnail uses. I found a fix online that requires changing one line of code in wp-includes/media.php:

    http://core.trac.wordpress.org/attachment/ticket/11846/media.php.diff

    I don’t normally like making code changes, but this fixed the problem for me.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘new post thumbnail function and alt attribute value’ is closed to new replies.