Skip to content

Instantly share code, notes, and snippets.

@rjv
Last active February 13, 2018 19:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rjv/305af6fe91575829ba46 to your computer and use it in GitHub Desktop.
Save rjv/305af6fe91575829ba46 to your computer and use it in GitHub Desktop.
Helper function for getting the featured image URL attached to a post (WordPress)
<?php
function get_thumbnail_url($post_id = null, $size = 'medium')
{
global $post;
if (is_string($post_id)) {
$size = $post_id;
$post_id = $post->ID;
}
if (is_null($post_id)) {
$post_id = $post->ID;
}
$thumbnailUrl = wp_get_attachment_image_src(get_post_thumbnail_id($post_id), $size, false, '');
if (is_array($thumbnailUrl)) {
return $thumbnailUrl[0];
}
return '';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment