Site logo

Archived topic

Fallback featured image

7 replies · Started by Kaleem on May 4, 2020

Viewing posts 1–8 of 8

Hello Sir, I have used your code found in a discussion:

add_filter( 'post_thumbnail_html', 'tu_post_thumbnail_fallback', 20, 5 );
function tu_post_thumbnail_fallback( $html, $post_id, $post_thumbnail_id, $size, $attr ) {
    if ( empty( $html ) ) {
        echo '<img src="URL TO YOUR FALLBACK IMAGE" alt="" />';
    }

    return $html;
}

The above code works and shows the image in archives but not shown in a single post.
Please kindly tell me what is wrong.
Thank you for your good work.

Hi there,

can you provide a link to your site ?

I have just emailed you.

Thank you in advance

Hi there,

Are you using other custom code for the featured images? There's an archive-thumb div which doesn't exist by default.

Yes, I have a custom template for displaying archive contents and the

archive-thumb

is used to show the images in the archive

How was the template created?

I just used the following codes it is fine now. Thank you for your support.

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

Note: In case someone is interested - please change bloginfo('template_directory') to bloginfo('stylesheet_directory') if you are using Child Theme.

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

Thanks once again for your supports.

Glad you got it working :)

This archived topic is closed to new replies.