[Resolved] Fallback featured image

Home Forums Support [Resolved] Fallback featured image

Home Forums Support Fallback featured image

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #1268337
    Kaleem

    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.

    #1268546
    David
    Staff
    Customer Support

    Hi there,

    can you provide a link to your site ?

    #1269273
    Kaleem

    I have just emailed you.

    Thank you in advance

    #1269690
    Tom
    Lead Developer
    Lead Developer

    Hi there,

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

    #1269797
    Kaleem

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

    archive-thumb

    is used to show the images in the archive

    #1270309
    David
    Staff
    Customer Support

    How was the template created?

    #1271473
    Kaleem

    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.

    #1272346
    Tom
    Lead Developer
    Lead Developer

    Glad you got it working 🙂

Viewing 8 posts - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.