SUBSCRIBE VIA RSS


Subscribe to our feed

Symfony Experts

Symfony Experts
If you have an urgent question for a symfony-related issue, this is the place to ask.

Topics

TWITTER

Stack Overflow


The old fashioned way

RECENT TUNES

February 12, 2010 – 6:11pm WordPress get post images and the_post_thumbnail caption

Before the_post_thumbnail was available, here is how I would fetch an array of images that were attached to a post in wordpress:

<?php $images = &get_children('post_type=attachment&post_mime_type=image&orderby=menu_order&order=ASC&post_parent='.$post->ID);
    if ($images):
    $image = current($images); $imageData = wp_get_attachment_image_src($image->ID); ?>
    <img src="<?php echo $imageData[0] ?>" alt="<?php the_title() ?>" />
    <?php endif ?>
<!-- image title: $image->post_title, caption: $image->post_excerpt, description: $image->post_content -->

Usually we’d use this just to pull out the first image attachment to a post and use this in a special place in our template. Now that there is built-in post thumbnail support, we wanted to use this feature but also still have the ability to display the selected thumbnail’s caption information. the_post_thumbnail only returns the image tag, so here is our custom filter (inside our template’s functions.php file) that adds our caption:

function monahans_thumbnail_caption($html, $post_id, $post_thumbnail_id, $size, $attr)
{
  $attachment =& get_post($post_thumbnail_id);
 
  // post_title => image title
  // post_excerpt => image caption
  // post_content => image description
 
  if ($attachment->post_excerpt || $attachment->post_content) {
    $html .= '<p class="thumbcaption">';
    if ($attachment->post_excerpt) {
      $html .= '<span class="captitle">'.$attachment->post_excerpt.'</span> ';
    }
    $html .= $attachment->post_content.'</p>';
  }
 
	return $html;
}
 
add_action('post_thumbnail_html', 'monahans_thumbnail_caption', null, 5);

The result of this filter is now anytime we call the_post_thumbnail in our template, the html will come back with our image caption, too.

Posted by in  Web Development   |  

4 Responses to WordPress get post images and the_post_thumbnail caption

  1. Rob says:

    Hello,

    Wonderful plugin, thanks. I spent a while looking for some way of displaying my captions.

    I also wanted to use the caption as a rollover state of a clickable thumbnail. I think this requires some code tweaks. Do you think this would even be possible?

    I could send the code in my functions file for my loop if that helps.
    Cheers

    Rob

  2. Rob says:
    function remove_index_loop() {
      remove_action('thematic_indexloop', 'thematic_index_loop');
    }
    add_action('init', 'remove_index_loop');
     
    function snippet_index_loop() {
      global $post;
            /* Count the number of posts so we can insert a widgetized area */ $count = 1;
            while ( have_posts() ) : the_post() ?&gt;
     
                &lt;div class=&quot;column "&gt;
     
                        &lt;div id=&quot;post-" class=""&gt;
     
     
     
     
    &lt;?php wp_link_pages(&#039;before=' .__('Pages:', 'thematic') . '&amp;after=') ?&gt;
                            
     
                        <!-- .post -->
                    <!-- .clear-fix -->
                <!-- .column -->
     
                        &lt;?php comments_template();
                        if ($count==$thm_insert_position) { get_sidebar(&#039;index-insert&#039;);}
     
     
            $count = $count + 1;
            endwhile;
    }
    add_action(&#039;thematic_indexloop&#039;, &#039;snippet_index_loop&#039;);
  3. Bill Mead says:

    Hey great function, but when I use it, the more tag no longer functions on the page, also my first article without a feature image gets no title displaying. Any ideas?

  4. Hansjörg says:

    Thanks for the code. In principle it works, but the caption is shown inside the a-tags and therefore it smashes the design.

    Any hints for me, how to put the caption outside the a-tags just below the thumbnail with the same css tags used in the single article?

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>