Skip to content

Instantly share code, notes, and snippets.

@deeman
Created September 23, 2019 07:02
Show Gist options
  • Select an option

  • Save deeman/4b8fd7454f05af89494f134c6ee952a4 to your computer and use it in GitHub Desktop.

Select an option

Save deeman/4b8fd7454f05af89494f134c6ee952a4 to your computer and use it in GitHub Desktop.
WordPress - Get Featured Image URL
// Post thumbnails are pretty useful and pretty easy to use in WordPress. Simply add:
add_theme_support('post-thumbnails');
// 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.
// It is also very easy to output that image as an HTML <img>:
the_post_thumbnail();
//Get 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 Within the loop, you'll have to do:
$thumb_id = get_post_thumbnail_id();
$thumb_url_array = wp_get_attachment_image_src($thumb_id, 'thumbnail-size', true);
$thumb_url = $thumb_url_array[0];
//Then $thumb_url will be that URL.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment