3

I'm trying to setup a default fallback featured image on my website to ensure consistency with images being displayed for every post, even when an image is not embedded in the post. I've tried a number of plugins - Default Post Thumb and Default Thumb, however neither of these seemed to work. I'm using WP 3.4 and the Suffusion Theme 4.2.2 on the site which can be seen at http://www.aliveradio.net/ I'm not very capable at editing basecode and would prefer to work with plugins, however if anyone could help with code alterations I'd be happy to give it a go.

Thank-you in advance for any help you can provide.

3 Answers 3

9

One simple method is to filter post_thumbnail_html, to add in a default image link:

<?php
function wpse55748_filter_post_thumbnail_html( $html ) {
    // If there is no post thumbnail,
    // Return a default image
    if ( '' == $html ) {
        return '<img src="' . get_template_directory_uri() . '/images/default-thumbnail.png" width="150px" height="100px" class="image-size-name" />';
    }
    // Else, return the post thumbnail
    return $html;
}
add_filter( 'post_thumbnail_html', 'wpse55748_filter_post_thumbnail_html' );
?>

You can make this filter more complex, but this should get you started.

4
  • Hi, Thanks for your feedback - I've looked at my functions.php document and can't see this argument anywhere - can you give me any advice as to where I might find this argument/ element? Commented Jun 17, 2012 at 22:13
  • What "argument/element" do you mean? Commented Jun 17, 2012 at 22:55
  • Where to add the code you've quoted above - do I create a new file or edit an existing one as I can't find post_thumbnail_html anywhere? I may have overlooked something as I'm new to this but any help is greatly appreciated. Commented Jun 19, 2012 at 8:31
  • Callbacks such as this one belong in your Theme's functions.php file. Commented Jun 19, 2012 at 13:22
0

while you say you'd like to use a plugin ... there is a very easy method to do what you want

basically inside your theme folder you will want to open and edit a file called single.php

you want to look for this code

the_post_thumbnail();

and replace it with this code (changing the path_to/default_image.jpg)

<?php if ( has_post_thumbnail() ) {  
the_post_thumbnail();  
} else { ?>  
<img src="<?php bloginfo('template_directory'); ?>/images/default-image.jpg" alt="<?php the_title(); ?>" />  
<?php } ?>`

If you find your site breaks / page doesn't load then you may need to remove the the opening

<?php and closing   ?>  

source: http://www.wpbeginner.com/wp-themes/how-to-set-a-default-fallback-image-for-wordpress-post-thumbnails/

Hope that helps?

Cheers

Damien

1
  • Solution works. And you do a great job of adding the default image in the active theme.
    – Pegues
    Commented Sep 6, 2018 at 12:12
-1

You can use if and else statement to do this, all you need to do is to manipulate the_post_thumbnail() function. You can do it like this

<?php if (has_post_thumbnail())
{ the_post_thumbnail(); 
} else { 
echo '<img src='" . '/image.png' . 'alt=no image'"/>'; ?>

You can take a look at http://techlog.xyz/how-to-set-default-featured-image-if-posts-has-none/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.