View difference between Paste ID: RxJjtJdP and 2EKc49jL
SHOW: | | - or go back to the newest paste.
1
// 05 jan 2013 alchymyth
2
// show thumbnail with post title as 'caption', wrapped with '.wp-caption thumb-caption' div;
3
4
add_filter('post_thumbnail_html','add_post_thumbnail_post_title_as_caption',10,5);
5
6
function add_post_thumbnail_post_title_as_caption($html, $post_id,
7
$post_thumbnail_id, $size, $attr) {
8
9
 if( $html == '' ) {
10
11
        return $html;
12
13
 } else {
14
15
        $out = '';
16
  global $post;
17
  $thumbnail_image = get_posts(array('p' => $post_thumbnail_id,
18
'post_type' => 'attachment'));
19
20
  if ($thumbnail_image && isset($thumbnail_image[0])) {
21
22
  $image = wp_get_attachment_image_src($post_thumbnail_id, $size);
23
  $t_width = $image[1] +0; // +0 here for no extra padding, needs to be considered in writing css - the default image caption uses +10;
24
  $class = $attr['class'];
25-
    if($thumbnail_image[0]->post_excerpt) $out .= '<div
25+
     $out .= '<div
26
class="wp-caption thumb-caption '.$class.'"
27
style="width:'.$t_width.'px; ">';
28
29
        $out .= $html;
30
31
        $out .= '<p
32
class="wp-caption-text">'.apply_filters('the_title',$post->post_title).'</p></div>';
33
  }
34
 return $out;
35
 }
36
}