Site logo

[Support request] Default / Fallback featured image

Home Forums Support [Support request] Default / Fallback featured image

Home Forums Support Default / Fallback featured image

Viewing 15 posts - 1 through 15 (of 23 total)
  • Author
    Posts
  • #505211
    Dan

    Hi
    Where can a default/fallback featured image be set up in case the content editor does not upload a featured image for the post?

    Thanks
    Dan

    #505358
    Tom
    Lead Developer
    Lead Developer

    Hi Dan,

    Something like this should work:

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

    Let me know 🙂

    #505555
    Dan

    Hi Tom,
    Thanks for your reply.
    Unfortunately, it doesn’t display anything.
    I tried both options (hardcoding the image and also via ACF):

    $default_img = get_field('default_img', 'options');
    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=' .$default_img .' alt="Pilatis" />';
    		 echo '<img src="http://localhost/qilates/wp-content/uploads/2018/02/default_thumbnail.jpg" alt="Pilatis" />';
        }
    
        return $html;
    }
    
    #505575
    Dan

    Does GP do a conditional check ‘has_thumbnail’?
    Maybe that’s the reason it’s not working?
    See “Notes about this method:” section in the below post:
    http://justintadlock.com/archives/2012/07/05/how-to-define-a-default-post-thumbnail

    Dan

    #505642
    Tom
    Lead Developer
    Lead Developer

    Try putting your $default_img variable inside the actual function.

    #505866
    Dan

    Thanks Tom,
    I removed it and hard coded the image for the purpose of the check, but it’s still not showing.

    /**default thumbnail for posts**/
    
    add_filter( 'post_thumbnail_html', 'tu_post_thumbnail_fallback', 20, 5 );
    
    //$default_img = get_field('default_img', 'options');
    
    function tu_post_thumbnail_fallback( $html, $post_id, $post_thumbnail_id, $size, $attr ) {
        if (empty($html) ) {
       //     echo '<img src=' .$default_img .' alt="Pilatis" />';
    echo '<img src="http://localhost/qilates/wp-content/uploads/2018/02/default_thumbnail.jpg" alt="Pilatis" />';
    		// echo '<img src="URL TO YOUR FALLBACK IMAGE" alt="" />';
        }
    
        return $html;
    }
    #506141
    Tom
    Lead Developer
    Lead Developer

    Link to the page where a default image should show?

    #506144
    Dan

    For example, this archive page
    http://qilates.s308.upress.link/category/natural-medicine/

    Thanks
    Dan

    #506530
    Tom
    Lead Developer
    Lead Developer

    Try this:

    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 ) ) {
            $html = '<img src="URL TO YOUR FALLBACK IMAGE" alt="" />';
        }
    
        return $html;
    }
    #506874
    Dan

    Sorry, still no go

    #507505
    Tom
    Lead Developer
    Lead Developer

    Let’s do this instead:

    add_filter( 'get_post_metadata', 'tu_fallback_featured_image', 10, 4 );
    function tu_fallback_featured_image( $value, $post_id, $meta_key, $single ) {
        if ( '_thumbnail_id' === $meta_key ) {
            if ( ! $value ) {
                $value = 1079; // ID of the fallback image.
            }
        }
     
        return $value;
    }
    #508938
    Dan

    Hi Tom
    This update just overrides all featured images and adds the default image to all the posts.
    It even replaces them in the featured image box.
    Also, it added the featured image on the top part of the homepage.

    Dan

    #509377
    Tom
    Lead Developer
    Lead Developer

    I just tested this plugin, and it works perfectly: https://wordpress.org/plugins/default-featured-image/

    Might be worth a shot.

    #510741
    Dan

    Thanks Tom, this plugin solves the issue, but I prefer solving it without a plugin so I have more control over the featured image, for specific categories, post types etc…
    It’s not super urgent but would like to know, for future project’s sake, if this can be done via code as our previous code efforts.

    Thanks
    Dan

    #510743
    Dan

    This plugin also has some filters:
    https://wordpress.org/plugins/default-featured-image/#faq

    Just curious why the code itself isn’t working with GP

    Dan

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