We're merging with GenerateBlocks! Learn more here.

[Support request] How to set a default fallback image if a post doesn’t include any image?

Please login to receive premium support.

Support for the free plugin can be found here.

Home Forums Pro Support How to set a default fallback image if a post doesn’t include any image?

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #25681
    Maria Rowena
    Participant

    Hello Support Team,

    I use carousel to display custom post in my website. Sometimes, there are post that don’t have featured image or any image at all. Is there a way that I can set a default fall back image if no featured image is set?

    As you know, the carousel doesn’t look good if a certain custom post doesn’t have image to display. Hope you can help me here. Thank you so much. πŸ™‚

    #25697
    elvin
    Moderator

    Hi,

    Is the fallback image a static image? If so, you can try this snippet:

    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;
    }

    where “URL TO YOUR FALLBACK IMAGE” is the path to your fallback image file.

    #25708
    Maria Rowena
    Participant

    Thank you for this code Elvin!

    I tried this one but it’s not working. I use carousel slider.

    See this screen shot. https://snipboard.io/v2QeZN.jpg.

    And below is the code we use. πŸ™‚

    `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 ‘listing-image‘;
    }

    return $html;
    }

    #25710
    elvin
    Moderator
    #25719
    Maria Rowena
    Participant

    Thank you for the suggestion Elvin, I use that plugin before but this time it is the custom post type that does not use the default featured image field of WordPress.

    Is there a way that we can set a default fall back image for the wp-show post list or carousel?

    Does the code you gave to me won’t work anymore?

    Looking forward for a positive response. Thank you so much! πŸ™‚

    #25738
    elvin
    Moderator

    Does the code you gave to me won’t work anymore?

    It doesn’t work because of this line of code within the theme.
    https://github.com/tomusborne/wp-show-posts/blob/35e410d7800273fc66f211c0f80d553e95d17f83/inc/functions.php#L154

    Commenting out this whole if ( ! has_post_thumbnail() ) {return;} will make my code work but that means you’ll have to edit the plugin’s code. While editing the plugin’s code can work, the issue is you’ll have to do this again once the plugin updates because plugin updates wipe any code changes you’ve made prior to it.

    You can try their code here to make the plugin work on custom post types.
    https://wordpress.org/support/topic/create-default-featured-image-for-one-custom-post-type-only/#post-13892814

    #25804
    Maria Rowena
    Participant

    Thank you so much! It works! πŸ™‚

    #25811
    elvin
    Moderator

    Nice one. Glad it works for you. πŸ™‚

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