Default Featured image in WordPress

Reading time ~3 minutes

This goes out to all the theme developers out there. EVERY SINGLE DESIGNER NEEDS TO IMPLEMENT THIS NOW

Featured images are great, unless the user doesn't add them. This solution will allow your template users to have engaging experience with your templates. If you are a template designer, it means more money for you.

Article can be read here: http://www.wpbeginner.com/wp-themes/how-to-set-a-default-fallback-image-for-wordpress-post-thumbnails/

Default WordPress Featured Images


WordPress has this very cool post thumbnail feature which was added in version 2.9. This feature basically allows you to add a custom image as a thumbnail that represents that post. This feature has become a standard for theme designers. But often, new bloggers forget to set a post thumbnail (featured image) which causes the template display to break. Second, it just looks unprofessional to have broken images or empty areas on homepage where the thumbnail was suppose to be. We believe that theme designers and developers should create a new standard of adding a default fallback image for WordPress post thumbnails in their themes. Popular frameworks like Genesis already has this type of option built-in where if the user does not specify a post thumbnail (featured image), then the first image attachment within the post will be displayed as the thumbnail. In this article, we will show you two different ways of adding a default fallback image for WordPress Post Thumbnails.

To utilize the tricks in this post, you have to enable the post thumbnails in your WordPress theme.

Adding a Branded Image as the Default Fallback


Often when creating a custom site for a client, they may have posts that has absolutely no image. So it is always smart for designers to create a branded image that is displayed for posts that has no thumbnail specified. The philosophy of this idea is similar to having a customized gravatar in WordPress. All you really have to do is open the theme file where you are going to display the post thumbnail, this can be (home.php, single.php, loop.php, index.php, archive.php, etc). Then simply paste the following code within the post loop.

<?php if ( has_post_thumbnail() ) {
the_post_thumbnail();
} else { ?>
<img src="<?php bloginfo('template_directory'); ?>/images/default-image.jpg" alt="<?php the_title(); ?>">
<?php } ?>
Explanation: The code checks if the post has a specified thumbnail, and if there is, then it will display the post thumbnail. If there is no post thumbnail specified, then it will display ‘default-image.jpg’ image from your theme’s images folder.

This is the very basic example. You can obviously expand on it with thumbnail sizes, class and much more.

Adding the First Post Image as the Default Fallback


Often new bloggers add images to the post, but either they forget to specify a post thumbnail, or they simply do not know how to. Regardless of what the case is, we as developers should prepare in advance. So the trick that we have seen used specially in theme frameworks like Genesis is adding the first post image as the default fallback for WordPress post thumbnails. To do this, you have to open your theme’sfunctions.php file and add the following codes:

<?
//function to call first uploaded image in functions file
function main_image() {
$files = get_children('post_parent='.get_the_ID().'&post_type=attachment
&post_mime_type=image&order=desc');
  if($files) :
    $keys = array_reverse(array_keys($files));
    $j=0;
    $num = $keys[$j];
    $image=wp_get_attachment_image($num, 'large', true);
    $imagepieces = explode('"', $image);
    $imagepath = $imagepieces[1];
    $main=wp_get_attachment_url($num);
		$template=get_template_directory();
		$the_title=get_the_title();
    print "<img src='$main' alt='$the_title' class='frame' />";
  endif;
}
Then open the theme file where you are displaying the WordPress post thumbnail, this can be (home.php, single.php, loop.php, index.php, archive.php, etc) and paste the following code:

<?php if (  (function_exists('has_post_thumbnail')) && (has_post_thumbnail())  ) {
  echo get_the_post_thumbnail($post->ID);
} else {
   echo main_image();
} ?>

Explanation: First we create a function that gets the first image attachment for each post. In our theme file code, we check if the post has a thumbnail. If the thumbnail exist, then we display the thumbnail, and if it does not exist, then we display the fallback.

Source: Snipplr

This is just the start. You can get creative with the fallback images for WordPress post thumbnails. You can combine the two techniques mentioned above, create branded images for each category, and much more.

If you are a theme developer, then make sure you have this in your themes.

Asyncio Basics Python 35

# Asyncio basics in pythonPython 3.5 brought with it asyncio. An event loop based paradigm previously available as a library but now it i...… Continue reading

Quick Laravel/Lumen install with Docker!

Published on March 02, 2016

PHP To Python

Published on February 24, 2016