Advertisement
alchymyth

caption on featured image

Apr 24th, 2012
563
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.28 KB | None | 0 0
  1. // 27/01/2012 alchymyth
  2. // show thumbnail with caption, if available, wrapped with '.wp-caption thumb-caption' div;
  3. // show just the thumbnail otherwise
  4. // for featured images in the loop
  5. add_filter('post_thumbnail_html','add_post_thumbnail_caption',10,5);
  6. function add_post_thumbnail_caption($html, $post_id, $post_thumbnail_id, $size, $attr) {  
  7.    
  8. if( $html == '' || !in_the_loop() ) {
  9.    
  10.     return $html;
  11.        
  12. } else {
  13.    
  14.     $out = '';
  15.  
  16.   $thumbnail_image = get_posts(array('p' => $post_thumbnail_id, 'post_type' => 'attachment'));
  17.  
  18.   if ($thumbnail_image && isset($thumbnail_image[0])) {
  19.  
  20.   $image = wp_get_attachment_image_src($post_thumbnail_id, $size);
  21.   $t_width = $image[1] +10; // +10 here for extra padding, needs to be considered in writing css - the default image caption uses +10;
  22.   $class = $attr['class'];
  23.     if($thumbnail_image[0]->post_excerpt) $out .= '<div class="wp-caption thumb-caption '.$class.'" style="width:'.$t_width.'px; ">';
  24.    
  25.     $out .= $html;
  26.    
  27.     if($thumbnail_image[0]->post_excerpt) $out .= '<p class="wp-caption-text">'.$thumbnail_image[0]->post_excerpt.'</p></div>';
  28.   }
  29. return $out;
  30. }
  31. /*css classes for this:
  32. .wp-caption.thumb-caption { ... }
  33. .wp-caption.thumb-caption img { ... }
  34. .wp-caption.thumb-caption .wp-caption-text { ... }
  35. */
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement