• Hello, I’m using the query loop block and the latest posts block. They can show the featured image. However they do not show the default featured image, if the theme has set one in functions.php.

    In other words, in functions.php I am using code like this:

    See in Github: https://gist.github.com/paaljoachim/df42fde2ed57b5589813

    /** Define a default post thumbnail */
    add_filter('genesis_get_image', 'default_image_fallback', 10, 2);
    function default_image_fallback($output, $args) {
        global $post;
        if( $output || $args['size'] == 'full' )
            return $output;
        $thumbnail = CHILD_URL.'/images/mydefaultimage.svg';
        switch($args['format']) {
            case 'html' :
                return '<img src="'.$thumbnail.'" class="defaultimage alignnone attachment-'. $args['size'] .'" alt="'. get_the_title($post->ID) .'" />';
                break;
            case 'url' :
                return $thumbnail;
                break;
           default :
               return $output;
                break;
        }
    }

    This successfully shows mydefaultimage.svg in ‘regular’ archives such as blog category pages when no other featured image is set for a post.

    However, when I insert the query loop block or latest posts block into the Block Editor, the default featured image does not show up at all.

    To confirm, posts with a featured image defined from the media library do show up with the query block or latest posts block.

    I have tried:

    To no avail.

    I have also tried googling this issue but to be honest, I can’t even think of the right words to Google to solve this. I have not found anything matching what I need nonetheless.

    My question:

    Does anyone know if something is wrong with the code above that causes the default featured image not to show in the query loop block or latest post block? Does anyone have alternative code I could use?

    What I do not want to do:

    • Use a plugin (if possible)
    • Remove the featured image
    • Use the first image in the post as the featured image
Viewing 2 replies - 1 through 2 (of 2 total)
  • I have used a plugin to use a default featured image and it worked without problems, but regarding your code, you can try this (not tested)

    add_filter( 'post_thumbnail_html', 'default_image_fallback', 10, 5 );
    function default_image_fallback( $html, $post_id, $post_thumbnail_id, $size, $attr ) {
        if ( ! $post_thumbnail_id && $size != 'full' ) {
            $thumbnail = CHILD_URL . '/images/mydefaultimage.svg';
            $html = '<img src="' . $thumbnail . '" class="defaultimage alignnone attachment-' . $size . '" alt="' . get_the_title( $post_id ) . '" />';
        }
        return $html;
    }
    
    Thread Starter joycegrace

    (@joycegrace)

    Thank you for your help. This code works for the query block, but then the default featured image doesn’t show up for ‘regular’ blog archives nor the latest posts block.

    Do you know if there is a code snippet that would work in all cases?

    Thank you again, I really appreciate your answer.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Display default featured image with query loop block / latest posts block?’ is closed to new replies.