Sunday, August 24, 2014

Wordpress: Get the url of the featured image

There is no super easy way to get the url of the featured image if you want to use it as a background image in inline css for example. You can add this handy function to your functions.php file however to help you:

 function getPostThumbnailUrl($id) {  
      $thumb_id = get_post_thumbnail_id($id);  
      $thumb_url_array = wp_get_attachment_image_src($thumb_id, 'thumbnail-size', true);  
      $thumb_url = $thumb_url_array[0];  
      return $thumb_url;  

You can use it from inside the loop without using the parameter, or you can get any posts featured image by adding the post's id...

No comments:

Post a Comment