Home Internet Blogging WordPress -Set Default Thumbnail Image For Posts

WordPress -Set Default Thumbnail Image For Posts


What is Default Image Thumbnail in WordPress

Default thumbnail of post is called dynamically when no thumbnail is used or uploaded for specific post . Suppose you have two posts with no thumbnail . Then if you set thumbnail for all posts ,a default image as thumbnail , all of two posts used this default image .

Functions required

has_post_thumbnail() is an wordpress built function and it is used to find that if post has a thumbnail or not .


the_post_thumbnail() is also a wordperss built in function to get the thumbnail image of post .




Code Example

To set default image create php code with loop . Following php code is an example code .


if ( has_post_thumbnail() ) {
else {
echo '<img src="' . get_bloginfo( 'stylesheet_directory' ) . '/images/No_image_available.jpg" />';

How the code works

In the following php statement

if (has_post_thumbnail( ))

check that , is the post has his own thumbnail or not. If statement “if(contition) is used to check the condition and return result . If the condition is true and function find that post has a thumbnail the first code block is executed and run function the_post_thumbnail() .


the_post_thumbnail('medium' );


This code find the specific id of thumbnail and display image of post. If condition is false and has_post_thumbnail() not find any thumbnail linked to post then second code block is executed .

else {
echo '<img src="' . get_bloginfo( 'stylesheet_directory' ) . '/images/No_image_available.jpg" />';

In this php code your default image is linked to post . get_bloginfo() function find the website path dynamically . You can also link img with a static url .

else {
echo '<img src="http://exampl.com/images/default-image.jpg" />';