Advertisement
ThomasKujawa

Thumb with Caption

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