Skip to content

Instantly share code, notes, and snippets.

@jonathanjanssens
Created July 14, 2016 14:04
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 jonathanjanssens/a21aed91c8aed0625582633307b53c3b to your computer and use it in GitHub Desktop.
Save jonathanjanssens/a21aed91c8aed0625582633307b53c3b to your computer and use it in GitHub Desktop.
Wordpress get post thumbnail url
<?php
/**
* Return the post thumbnail URL
*
* @param string $size thumbnail size name
* @param \WP_Post|int $post wordpress post object
* @return string|boolean the post thumbnail url, the fallback url or false
*/
function get_post_thumbnail_url($size = 'post-thumbnail', $post = null) {
if($post === null) {
global $post;
} else {
if(is_numeric($post)) {
$post = get_post($post);
}
}
if(has_post_thumbnail($post->ID)) {
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), $size );
if($image) {
return $image[0];
}
}
return '';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment