Last active
October 7, 2015 02:57
-
-
Save jaredkc/3093763 to your computer and use it in GitHub Desktop.
WordPress Theme : Check for featured image
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
/* | |
Add the following to your theme's function.php file | |
Useful when you have multiple excerpt and/or content files | |
*/ | |
// Check to see if the post has a featured image then output HTML | |
// Then add a class to via post_class if it does | |
function my_post_thumbnail($thumb_size = 'thumbnail') { | |
if ( has_post_thumbnail() ) { | |
ob_start(); | |
?> | |
<a href="<?php the_permalink() ?>"><?php the_post_thumbnail($thumb_size); ?></a> | |
<?php | |
$thumb = ob_get_clean(); | |
} | |
else { | |
$thumb = ''; | |
} | |
return $thumb; | |
} | |
function my_post_thumbnail_class($class = '') { | |
if ( has_post_thumbnail() ) { | |
$class[] = 'has-thumbnail'; | |
} | |
return $class; | |
} | |
add_filter('post_class', 'my_post_thumbnail_class'); | |
/* | |
Then add this to your template files | |
*/ | |
echo my_post_thumbnail('enter-size-here'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment