Created
July 14, 2016 14:04
-
-
Save jonathanjanssens/a21aed91c8aed0625582633307b53c3b to your computer and use it in GitHub Desktop.
Wordpress get post thumbnail url
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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