Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

This seems to be an easy one but its giving me quite a hard time. get_the_post_thumbnail() suppose to take attribute as an array. Well, everything working right but when i pass style attr its doesn't work.

$attr = array(
                                'title' => get_the_title(),
                                'alt' => get_the_title(),
                                'style' => 'float:left'
                            );

$thumb = get_the_post_thumbnail($post->ID, 'large-thumb', $attr);

The title and the alt attribute is set up nicely though but style attribute is missing in images.

FYI: I am showing the thumbnail in rss feed.

share|improve this question
add comment

1 Answer 1

up vote 4 down vote accepted

get_the_post_thumbnail attribute array doesn't know STYLE , the fields that are available for you to use are:

  • src
  • class
  • alt
  • title

so just use the class and define you class to it:

$attr = array(
                                'title' => get_the_title(),
                                'alt' => get_the_title(),
                                'class' => 'rss_thumb'
                            );
$thumb = get_the_post_thumbnail($post->ID, 'large-thumb', $attr);

and then define the style in the class:

<style>
.rss_thumb{float:left}
</style>

Update:

My bad rss feed can really be styled like HTML since its not HTML. so to over come this problem you need to have your image inside the content tag and give it an align="left" which should work in most rss readers.

so what you really want is to add a content_filter:

add_filter('the_content','add_rss_thumb');

function add_rss_thumb($content){
    if (!is_feed()){
        return $content;
    }

    //now that we know is a feed we add the image to the content:
    global $post;
    if(has_post_thumbnail()) {
        $post_thumbnail_id = get_post_thumbnail_id( $post->ID );
        $thumbnail_attributes = wp_get_attachment_image_src( $post_thumbnail_id );
        /*
            $thumbnail_attributes is an array containing: 
            [0] => url
            [1] => width
            [2] => height 
        */

        return '<img src="'.thumbnail_attributes[0].'" title="'.the_title_attribute('','',0).'" alt="'.the_title_attribute('','',0).'" align="left">'.$content;
    }
    return $content;
}
share|improve this answer
    
It seems. Wordpress feed does not echo any style attribute directly in html element. –  Sisir Jun 27 '11 at 11:17
    
Note also, for the title attribute, you should be using get_the_title_attribute(). –  Chip Bennett Jun 27 '11 at 11:23
    
How do i add styles in my rss feed? –  Sisir Jun 27 '11 at 11:30
    
i updated my answer –  Bainternet Jun 27 '11 at 12:24
    
Thanks, Worked! –  Sisir Jun 27 '11 at 14:06
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.