Get Featured Image URL


SUBMITTED BY: phpsnippets

DATE: Oct. 22, 2015, 3:25 p.m.

FORMAT: Text only

SIZE: 778 Bytes

HITS: 1478

  1. Post thumbnails are pretty useful and pretty easy to use in WordPress. Simply add:
  2. add_theme_support('post-thumbnails');
  3. To a theme's functions.php file and you'll get a Featured Image module on the admin screen for posts which allows you to select one.
  4. It is also very easy to output that image as an HTML <img>:
  5. the_post_thumbnail();
  6. But what if you just need the URL? Say, you're going to use it as a background-image on an element rather than a content image. Unfortunately there is no super easy/obvious function for that.
  7. Within the loop, you'll have to do:
  8. $thumb_id = get_post_thumbnail_id();
  9. $thumb_url_array = wp_get_attachment_image_src($thumb_id, 'thumbnail-size', true);
  10. $thumb_url = $thumb_url_array[0];
  11. Then $thumb_url will be that URL.

comments powered by Disqus