daily pastebin goal
63%
SHARE
TWEET

WordPress Find Featured Image or First Image in Post, etc

amberweinberg Jan 25th, 2017 (edited) 354 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3.     /*
  4.  
  5. These functions are great for WordPress sites with posts and media that have been imported. Some images are         featured images, some in the content itself, and of the content images, some are internal, others external. These images are grabbed to use at the correct size for archive/homepage thumbnails*/
  6.  
  7.         1) Look for featured image, show if present
  8.         2) Otherwise look for the first image in the content (whether internal or external)
  9.         3) Check for an attachment ID, if present, show image at correct dimensions
  10.         4) Otherwise show image at normal URL
  11.  
  12.         $size = post thumbnail / custom image sizes
  13.         $url = return a URL or full image tag
  14. */
  15.  
  16.  
  17.         /*Find the image id from a URL*/
  18.  
  19. function url_get_image_id($image_url) {
  20.     global $wpdb;
  21.     $attachment = $wpdb->get_col($wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE guid='%s';", $image_url ));
  22.     return $attachment[0];
  23. }
  24.  
  25. /* determine whether post has a featured image, if not, find the first image inside the post content, $size passes the thumbnail size, $url determines whether to return a URL or a full image tag*/
  26.  
  27. function checkImageType($size, $type) {
  28.    
  29.     global $post;
  30.     $content = $post->post_content;                
  31.     $first_img = '';
  32.     ob_start();
  33.     ob_end_clean();
  34.     $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $content, $matches);
  35.     $first_img = $matches[1][0];
  36.    
  37.     /*If there's a featured image, show it*/
  38.            
  39.     if (get_the_post_thumbnail($post_id) != '' ) {
  40.         if($type=='url') {
  41.             the_post_thumbnail_url($size);
  42.         } else {
  43.             the_post_thumbnail($size);
  44.         }
  45.     } else {
  46.        
  47.         /*No featured image, so we get the first image inside the post content*/
  48.        
  49.         if ($first_img) {
  50.            
  51.             //let's get the correct image dimensions
  52.  
  53.             $image_id = url_get_image_id($first_img);
  54.             $image_thumb = wp_get_attachment_image_src($image_id, $size);
  55.            
  56.             // if we've found an image ID, correctly display it
  57.            
  58.             if($image_thumb) {
  59.                 if($type=='url') {
  60.                     echo $image_thumb[0];
  61.                 } else {
  62.                    
  63.                     echo '<img src="'.$image_thumb[0].'" alt="'.get_the_title().'"/>';
  64.                 }
  65.             } else {
  66.                
  67.                 //if no image (i.e. from an external source), echo the original URL
  68.                
  69.                 if($type=='url') {
  70.                     echo $first_img;
  71.                 } else {
  72.                    
  73.                     echo '<img src="'.$first_img.'" alt="'.get_the_title().'"/>';
  74.                 }
  75.                                
  76.             }
  77.         }
  78.     }
  79. }
  80.  
  81. // Sample Uses
  82.  
  83. checkImageType('full', 'url');
  84.  
  85. // Returns: http://domain.com/image-url.jpg)
  86.  
  87. checkImageType('post-thumb');
  88.  
  89. // Returns: <img src="http://domain.com/image-url.jpg" alt="Alt text">
  90.  
  91. ?>
RAW Paste Data
Top