Guest User

Untitled

a guest
Jun 19th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.60 KB | None | 0 0
  1. function get_first_image() {
  2.     global $post;
  3.     $PostID = $post->ID;
  4.    
  5.     $all_images =& get_children('post_type=attachment&post_mime_type=image&post_parent=' . $PostID );
  6.      
  7.     if($all_images) {
  8.         $arr_of_all_images = array_keys($all_images);
  9.          
  10.         // Get the first image attachment from post
  11.         $firstImage = $arr_of_all_images[0];
  12.          
  13.         // Get the thumbnail url for the attachment
  14.         // If you want the full-size image instead of the thumbnail, use wp_get_attachment_url() instead of wp_get_attachment_thumb_url().
  15.         $thumb_url = wp_get_attachment_thumb_url($firstImage);
  16.          
  17.         echo $thumb_url;
  18.     }
  19. }
Add Comment
Please, Sign In to add comment