WordPress.org

Ready to get started?Download WordPress

Forums

Getting the post title to show as a title attribute of the thumbnail (5 posts)

  1. Amy
    Member
    Posted 2 years ago #

    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 )),)); ?>

  2. Amy
    Member
    Posted 2 years ago #

    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?

  3. OrT
    Member
    Posted 2 years ago #

    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:

    <?php
       $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'); ?>" />
  4. Amy
    Member
    Posted 2 years ago #

    Hello,

    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.

  5. alchymyth
    The Sweeper & Moderator
    Posted 2 years ago #

    try:

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

Topic Closed

This topic has been closed to new replies.

About this Topic