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 in  Web Development   |     |  delicious  Digg

2 Responses to “Wordpress get post images and the_post_thumbnail caption”

  1. 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

    By Rob on Mar 27, 2010

  2. 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;);

    By Rob on Mar 27, 2010

Post a Comment

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word