WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Display Alt Text under a Post Thumbnail (6 posts)

  1. photocurio
    Member
    Posted 2 years ago #

    I got captions to load under thumbnail images with mosdave's function posted here: Display caption with the_post_thumbnail (this thread is now closed).

    Mosdave's code works fine for displaying the title, caption, or description, but it won't display the alt text, as suggested in the last lines.

    Mosdave's function is here:

    function the_post_thumbnail_caption() {
      global $post;
    
      $thumb_id = get_post_thumbnail_id($post->id);
    
      $args = array(
    	'post_type' => 'attachment',
    	'post_status' => null,
    	'post_parent' => $post->ID,
    	'include'  => $thumb_id
    	); 
    
       $thumbnail_image = get_posts($args);
    
       if ($thumbnail_image && isset($thumbnail_image[0])) {
         //show thumbnail title
         echo $thumbnail_image[0]->post_title; 
    
         //Uncomment to show the thumbnail caption
         //echo $thumbnail_image[0]->post_excerpt; 
    
         //Uncomment to show the thumbnail description
         //echo $thumbnail_image[0]->post_content; 
    
         //Uncomment to show the thumbnail alt field
         //$alt = get_post_meta($thumbnail_id, '_wp_attachment_image_alt', true);
         //if(count($alt)) echo $alt;
      }
    }
  2. alchymyth
    The Sweeper & Moderator
    Posted 2 years ago #

    try:

    $alt = get_post_meta($thumbnail_image[0]->ID, '_wp_attachment_image_alt', true);
  3. Digital Raindrops
    Member
    Posted 2 years ago #

    It's a typo $thumb_id has a value, and $thumbnail_id has never been set!

    $thumb_id = get_post_thumbnail_id($post->id);
    
    //Uncomment to show the thumbnail alt field
    //$alt = get_post_meta($thumbnail_id, '_wp_attachment_image_alt', true);

    Should be:

    //Uncomment to show the thumbnail alt field
    //$alt = get_post_meta($thumb_id, '_wp_attachment_image_alt', true);

    HTH

    David

  4. photocurio
    Member
    Posted 2 years ago #

    thanks for finding that typo.
    Here is another use for the function: adding linked photo credits. See it here in action: Newsprint Theme.

    To do this I used the plugin Media Custom Fields. This plugin is buggy, but if you read the forum posts on it, your can fix it.

    Then I made a media custom field called Credit, and one called Credit URL. This keeps everything semantic.

    I used the following function:

    function the_post_thumbnail_credit() {
      global $post;
      $thumb_id = get_post_thumbnail_id($post->id);
      $args = array(
    	'post_type' => 'attachment',
    	'post_status' => null,
    	'post_parent' => $post->ID,
    	'include'  => $thumb_id
    	);
       $thumbnail_image = get_posts($args);
       if ($thumbnail_image && isset($thumbnail_image[0])) {
         $credit 	= get_post_meta($thumb_id, 'tqmcf_photo-credit', true);
    	 $crediturl = get_post_meta($thumb_id, 'tqmcf_photo-credit-url', true);
         if(count($crediturl)) echo '<p class="photocredit"><a href="'. $crediturl . '">';
    	 if(count($credit)) echo $credit;
    	 if(count($crediturl)) echo '</a></p>';
      }
    }

    Both credit and link are added to the loop with this:
    the_post_thumbnail_credit()

  5. Digital Raindrops
    Member
    Posted 2 years ago #

    Nice column layout, did you use post classes?

    I used them recently for different layouts per category, you might find something handy as it only uses one WordPress post image size for all seven different layouts.

    Photography and news like your theme needs a clean simple look, with dashed borders, here is the twenty eleven child photo/magazine theme!

    HTH

    David

  6. photocurio
    Member
    Posted 2 years ago #

    Thanks, David. I have not yet implemented post classes in this one.. I'm not sure they will be needed.

Topic Closed

This topic has been closed to new replies.

About this Topic