[Support request] Post without featured image

Please login to receive premium support.

Support for the free plugin can be found here.

Home Forums Pro Support Post without featured image

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #24297
    paul
    Participant

    Hello,

    I’m looking at moving away from featured images, is there a way to tell wpshowposts to use a different thumbnail image if no featured image exists?

    Thnaks

    #24342
    elvin
    Moderator

    Hi,

    You can follow Tom’s advice here: https://generatepress.com/forums/topic/posts-page-show-attachment-image-if-no-featured-image/#post-245909

    Which basically is filtering how the post thumbnail displays.

    #24353
    paul
    Participant

    Thanks – I don’t really understand how to set this up. Happy enough to add the function but no idea how I tell it which image to pull. Could it be setup to pull an image via a custom field?

    #24359
    elvin
    Moderator

    Could it be setup to pull an image via a custom field?

    Sure that’s possible.

    The code will look something like this:

    add_filter( 'post_thumbnail_html', 'my_post_thumbnail_fallback', 20, 5 );
    function my_post_thumbnail_fallback( $html, $post_id, $post_thumbnail_id, $size, $attr ) {
        $fallback_image = get_field('fallback_image');
        if ( empty( $html ) ) {
            $html = sprintf( '<img src="%1$s" alt="%2$s" itemprop="image" class="%3$s" />',
                WPSP_Resize( $fallback_image, $image_atts[ 'width' ], $image_atts[ 'height' ], $image_atts[ 'crop' ], true, $image_atts[ 'upscale' ] )
                esc_attr( the_title() ),
                $settings[ 'image_alignment' ]
            );
        }
        return $html;
    }

    The fallback_image within get_field('fallback_image') is basically the slug of the fallback image custom field. This is assuming you’re using ACF.

    #24362
    paul
    Participant

    OK that looks like it could work well. Thanks for your help. I probably won’t implement this until next year and will get back to you if anything comes up.

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