Site logo

Archived topic

Change single page featured image

5 replies · Started by Mathieu on November 18, 2019

Viewing posts 1–6 of 6

Hi Tom,

I am using add_image_size() to create a custom size that won't waste bandwith/time for anybody.

How can I hook into the code of the featured image (in the single page) so that the get_the_post_thumbnail() function uses my custom size?

Or maybe there's a feature somewhere that I am not seeing.

Best regards,

Well I was really looking into the hook. On the homepage, I do it easily a piece of code Tom gave me :

add_filter('generate_featured_image_output', function () {
    $isHot = false;
    if (isHot(get_the_id())) {
        $isHot = '<span class="hotpost"></span><span class="hottexte">En vedette!</span>';
    }
    return sprintf( // WPCS: XSS ok.
        '<div class="post-image">
            %1$s
            <a href="%2$s">
                %3$s
            </a>
        </div>',
        $isHot,
        esc_url(get_permalink()),
        get_the_post_thumbnail(
            get_the_ID(),
            apply_filters('generate_page_header_default_size', '<strong>liste</strong>'),
            array(
                'itemprop' => 'image',
            )
        )

    );
});

But I guess that *could* do the same and be much cleaner.

However, I have a few issue with using that built-in feature.

1) I have no "image_processing_queue" cron events.

2) When I am looking at the lists of available thumbnail I have inside the theme, I have nothing matching what I put in « Customize / Layout / Blog / Featured Image / Posts »

So either I have something broken or it won't play well with the image optimization tools I use.

You should just be able to do this:

add_filter( 'generate_page_header_default_size', function( $size ) {
    if ( is_page() ) {
        $size = 'your-custom-size';
    }

    return $size;
} );

I'm not a fan of the current image resizer, either. I have a better idea I'll be implementing in 2020 :)

Ahhh thank you very much Tom. I am not sure why it didn't work yesterday as I was testing something similar.

Oh well, thanks again and have a nice day :)

You're welcome! You too :)

This archived topic is closed to new replies.