Last active
July 12, 2016 08:32
-
-
Save morgyface/720f760c46fcbf3a5617ee6ae459ba61 to your computer and use it in GitHub Desktop.
WordPress | If no featured image show a generic image
This file contains hidden or 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 | |
| $title = get_the_title(); | |
| $thumbnail = get_the_post_thumbnail(); | |
| echo '<img src="'; | |
| if ( $thumbnail ) { | |
| $imageid = get_post_thumbnail_id(); | |
| $imagesrc = wp_get_attachment_image_src($imageid, 'full'); | |
| echo $imagesrc[0] . '" alt="'; | |
| } else { | |
| $themefolder = get_bloginfo('template_directory'); | |
| echo $themefolder . '/images/no-image.png" alt="'; | |
| } | |
| echo $title . '">' . PHP_EOL; | |
| ?> |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
No image, no problem muthalicker
Pretty basic stuff but nice to have on file. Basically we check to see if the current post has a featured image. If it does then great, we grab the ID of the image and then the source so we can echo the URL. If not, then we grab a generic image from the active theme's images folder.
This could go in single.php or be inside a get_posts loop within another template.