5

i use this code to show post's thumbnail in my site but this code cant show the title Attribute of the thumbnails.

how can i add Title Attribute to WordPress thumbnails?

<?php if ( has_post_thumbnail() ) {
the_post_thumbnail('large'); 
 } else {?>
<img alt="<?php the_title(); ?>" title="<?php the_title(); ?>" src="<?php bloginfo('template_url'); ?>/img/thumbnail.png"/>
<?php }?>

you can see my site with this url : http://rokesh.ir

4

You can do that because you can add all the attributes you need:

the_post_thumbnail( 'large', array( 'title' => get_the_title() ) ); 

Please read on in the Function Reference of the_post_thumbnail.

  • @Chip Bennett was quicker than me. What is to do in this case? Should I delete my answer? – realloc Oct 29 '13 at 18:14
3

As per the Codex entry for the_post_thumbnail(), you can pass an attributes array as a parameter:

<?php the_post_thumbnail( $size, $attr ); ?>

So you would just need to define the array; here's the Codex example, modified to include the title attribute:

$post_thumbnail_attr = array(
    'src'   => $src,
    'class' => "attachment-$size",
    'alt'   => 'alt here',
    'title' => 'title here',
);

...which you could then pass to the_post_thumbnail():

the_post_thumbnail( 'large', $post_thumbnail_attr );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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