Support » Themes and Templates » Getting the post title to show as a title attribute of the thumbnail

  • Hello,

    I have a slideshow showing the first 4 news posts. I want to output the featured image (thumbnail) to have a title attribute which is the post title. So far, using the code bellow the closest I’ve got it is to just remove the title … can anyone tell me where I’m going wrong with this array and what I need to do to get it to display the post’s title?

    Thanks for any help.

    <?php the_post_thumbnail('', array('title' => trim(strip_tags( $attachment->post_title )),)); ?>

Viewing 4 replies - 1 through 4 (of 4 total)
  • Looking at this again, I can see I’ve misunderstood what that array was doing. It’s stripping away the title tag content … so to revise my question, how could I instead input the post’s title there?

    Hi Amy,

    This should work:

    <?php the_post_thumbnail('', 'title='.trim(strip_tags( $attachment->post_title ))); ?>

    If not, this might be a workaround based on something similar I did in the past:

       $thumbnail_src = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ) );
    <img src="<?php echo $thumbnail_src; ?>" title="<?php the_title_attribute('echo=0'); ?>" />


    Sorry for the delayed response, I was away.

    Thanks for these suggestions, unfortunately neither worked. The first had the same result, showing the filename of the photo. The second workaround didn’t work at all, it won’t display the thumbnail or the title.



    Forum Moderator


    <?php the_post_thumbnail('', array('title' => esc_attr( $post->post_title ))); ?>

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Getting the post title to show as a title attribute of the thumbnail’ is closed to new replies.