• Resolved photocurio

    (@photocurio)


    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;
      }
    }

Viewing 5 replies - 1 through 5 (of 5 total)
  • try:

    $alt = get_post_meta($thumbnail_image[0]->ID, '_wp_attachment_image_alt', true);

    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

    Thread Starter photocurio

    (@photocurio)

    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()

    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

    Thread Starter photocurio

    (@photocurio)

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

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Display Alt Text under a Post Thumbnail’ is closed to new replies.