SHARE
TWEET

POST THUMBNAIL AND CAPTION

alchymyth Feb 1st, 2015 226 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //POST THUMBNAIL AND CAPTION STYLED SIMILAR TO .wp-caption//
  2. function the_post_thumbnail_and_caption($size = '', $attr = '') {
  3. global $post;
  4. $output = '';
  5. $thumb_id = get_post_thumbnail_id($post->ID);
  6.     $args = array(
  7.         'post_type' => 'attachment',
  8.         'post_status' => null,
  9.         'parent' => $post->ID,
  10.         'include'  => $thumb_id
  11.     );
  12.  
  13. $thumbnail_image = get_posts($args);
  14.  
  15. if ($thumb_id && $thumbnail_image && isset($thumbnail_image[0])) {
  16.     $image = wp_get_attachment_image_src( $thumb_id, $size );
  17.     $image_width = $image[1];
  18.  
  19.     $attr_class = '';
  20.         if($attr && isset($attr['class'])) $attr_class = $attr['class'];
  21.     $attr['class'] = ''; //move any 'class' attributes to the outer div, and remove from the thumbnail
  22.  
  23.     $output = '<div class="thumbnail-caption attachment-'.$size.($attr?' '.$attr_class:'').'" style="width: ' . ($image_width) . 'px">';
  24.  
  25.     $output .= get_the_post_thumbnail($post->ID, $size, $attr);
  26.  
  27.     /* to show the thumbnail caption */
  28.     $caption = $thumbnail_image[0]->post_excerpt;
  29.     if($caption) {
  30.         $output .= '<p class="thumbnail-caption-text">';
  31.         $output .= $caption;
  32.         $output .= '</p>';
  33.     }
  34.  
  35.     /* //Uncomment to show thumbnail title
  36.     $title = $thumbnail_image[0]->post_title;
  37.     if($title) {
  38.         $output .= '<p class="thumbnail-title-text">';
  39.         $output .= $title;
  40.         $output .= '</p>';
  41.     } */
  42.  
  43.     /* //Uncomment to show the thumbnail description
  44.     $descr = $thumbnail_image[0]->post_content;
  45.     if($descr) {
  46.         $output .= '<p class="thumbnail-description-text">';
  47.         $output .= $descr;
  48.         $output .= '</p>';
  49.     } */
  50.  
  51.     /* //Uncomment to show the thumbnail alt field
  52.     $alt = get_post_meta($thumb_id, '_wp_attachment_image_alt', true);
  53.     if(count($alt)) {
  54.         $output .= '<p class="thumbnail-alt-text">';
  55.         $output .= $alt;
  56.         $output .= '</p>';
  57.     } */
  58.  
  59.     $output .= '</div>';
  60.     }
  61. echo $output;
  62. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top