Advertisement
josh_max

Image from Post (Feat'd, Attach'd, 1st, YouTube, Default)

Sep 14th, 2012
1,449
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.13 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4.  * This function will get the first image in this order:
  5.  *
  6.  *    1) Featured image
  7.  *    2) First image attached to post
  8.  *    3) First image URL in the content of the post
  9.  *    4) YouTube screenshot
  10.  *    5) Default images for different categories [SET MANUALLY]
  11.  *    6) Backup-default image if all else fails [SET MANUALLY]
  12.  *
  13.  * A big THANKS goes out to Michelle @ WordPress.StackExchange.com  
  14.  * for her answer (http://bit.ly/O3UaLm) that made this possible!!!
  15.  *
  16.  */
  17.  
  18. //
  19. // Activate post-thumbnails by adding this to theme's function.php file
  20. //
  21. add_theme_support('post-thumbnails');
  22.  
  23.  
  24. //
  25. // Add the following to the theme's function.php file
  26. //
  27. function vp_get_thumb_url($text, $size){
  28.     global $post;
  29.     $imageurl="";
  30.  
  31. // 1) FEATURED IMAGE
  32.     // Check to see which image is set as "Featured Image"
  33.     $featuredimg = get_post_thumbnail_id($post->ID);
  34.     // Get source for featured image
  35.     $img_src = wp_get_attachment_image_src($featuredimg, $size);
  36.     // Set $imageurl to Featured Image
  37.     $imageurl=$img_src[0];
  38.  
  39. // 2) 1ST ATTACHED IMAGE IMAGE
  40.     // If there is no "Featured Image" set, move on and get the first image attached to the post
  41.     if (!$imageurl) {
  42.         // Extract the thumbnail from the first attached imaged
  43.         $allimages =&get_children('post_type=attachment&post_mime_type=image&post_parent=' . $post->ID );
  44.  
  45.         foreach ($allimages as $img){
  46.             $img_src = wp_get_attachment_image_src($img->ID, $size);
  47.             break;
  48.         }
  49.         // Set $imageurl to first attached image
  50.         $imageurl=$img_src[0];
  51.     }
  52.  
  53. // 3) 1ST IMAGE URL IN POST
  54.     // If there is no image attached to the post, look for anything that looks like an image and get that
  55.     if (!$imageurl) {
  56.         preg_match('/<\s*img [^\>]*src\s*=\s*[\""\']?([^\""\'>]*)/i' ,  $text, $matches);
  57.         $imageurl=$matches[1];
  58.     }
  59.  
  60. // 4) YOUTUBE SCREENSHOT
  61.     // If there's no image attached or inserted in the post, look for a YouTube video
  62.     if (!$imageurl){
  63.         // look for traditional youtube.com url from address bar
  64.         preg_match("/([a-zA-Z0-9\-\_]+\.|)youtube\.com\/watch(\?v\=|\/v\/)([a-zA-Z0-9\-\_]{11})([^<\s]*)/", $text, $matches2);
  65.         $youtubeurl = $matches2[0];
  66.         $videokey = $matches2[3];
  67.     if (!$youtubeurl) {
  68.         // look for youtu.be 'embed' url
  69.         preg_match("/([a-zA-Z0-9\-\_]+\.|)youtu\.be\/([a-zA-Z0-9\-\_]{11})([^<\s]*)/", $text, $matches2);
  70.         $youtubeurl = $matches2[0];
  71.         $videokey = $matches2[2];
  72.     }
  73.     if ($youtubeurl)
  74.         // Get the thumbnail YouTube automatically generates
  75.         // '0' is the biggest version, use 1 2 or 3 for smaller versions
  76.         $imageurl = "http://i.ytimg.com/vi/{$videokey}/0.jpg";
  77.     }
  78. // 4) YOUTUBE SCREENSHOT
  79.     // If there's no YouTube video in the post, look for the default based on the category
  80.     if (!$imageurl) {
  81.         // Set default Image for different categories
  82.         // [SET DIRECTORY MANUALLY!!]
  83.         $dir = get_template_directory_uri() . '/images/'; // [SET MANUALLY!!!]
  84.         // [DID YOU SET YOUR DIRECTORY?!]
  85.  
  86.         $get_cat = get_the_category();
  87.         $cat = $get_cat[0]->
  88.         slug;
  89.         // [SET IMG EXT MANUALLY!!]
  90.         $imageurl = $dir . $cat . '.jpg'; // [SET MANUALLY!!!]
  91.         // [DID YOU SET YOUR IMG EXT?!]
  92.  
  93.         // Use this array if you have a few main categories that you want images for
  94.         $array = array( 'cat_1', 'cat_2', 'cat_3',);
  95.         if (!in_array($cat, $array))
  96.             // [SET BACKUP IMAGE MANUALLY!!!]
  97.             $imageurl = $dir . 'district.jpg'; // [SET MANUALLY!!!]
  98.             // [DID YOU SET YOUR BACKUP IMAGE?!]
  99.     }
  100.  
  101.     // Spit out the image path
  102.     return $imageurl;
  103. }
  104.  
  105.  
  106. //
  107. // Add inside loop but before displaying image
  108. //
  109. if (function_exists('vp_get_thumb_url')) {
  110.     // Set the desired image size.
  111.     // Swap out 'thumbnail' for 'medium', 'large', or '[your-custom-size]'
  112.     $thumb=vp_get_thumb_url($post->post_content, 'large-feature');
  113. }
  114.  
  115.  
  116. //
  117. // Call the image inside the loop
  118. //
  119. ?>
  120. <a href="<?php get_permalink(); ?>" title="<?php the_title_attribute(); ?>" class="featured-image">
  121.     <img src="<?php
  122.         // Gets the image URL
  123.         if ($thumb!='') echo $thumb;
  124.     ?>" class="attachment-thumbnail wp-post-image" />
  125. </a>
  126.  
  127. <?php
  128. /*
  129.  * A big THANKS goes out to Michelle @ WordPress.StackExchange.com
  130.  * for her answer (http://bit.ly/O3UaLm) that made this possible!!!
  131.  *
  132.  */
  133. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement