Site logo

Archived topic

How to create post template with different featured image size?

15 replies · Started by Evenit on July 8, 2020

Viewing posts 1–15 of 16

Hi I have installed GP theme on my site. With my previuos theme I could choose to have 2 post templates which let me use Featured Image in 2 different dimensions (880 and 1326). Now I would need to replicate the same behaviour since my images are all in both sizes. How can I do it?

Thanks

Hi Tom, thank you for your reply. Maybe I badly explained myself.

Pleasee se these 2 examples from my website:

Example 1 with bigger featured image: https://www.viaggi-usa.it/antelope-island-state-park/
Example 2 with smaller featured image: https://www.viaggi-usa.it/noleggio-auto-usa/

These are not custom template I added in the child theme, but an option of my current theme, which let you choose the dimension of the featured image for each post.

I hope now it is clear. Thank you for your assistance.

Hi there,

the decision to use the bigger or smaller image - is this purely down to individual choice or is there any other relationship to a post - such as a Category or Tag?

Just looking for the easiest way to allow you to choose an option.

Let us know.

It is purely down to individual choice

And how would you make that choice? An option in the editor?

There needs to be some sort of mechanism so the page knows what size to load. Maybe a custom field?

Hi Tom, there is an option inside the article editor called "Post Layout", please see the screenshot.
As you can see this option change many things but for me the most important is the Featured Image.
Thanks

So that metabox will set a custom field.

We don't have a metabox like that, but you can use custom fields: https://www.wpbeginner.com/wp-tutorials/wordpress-custom-fields-101-tips-tricks-and-hacks/

Then you can tell GP to use a different image size if the custom field is set:

add_filter( 'generate_page_header_default_size', function( $size ) {
    $custom_field = get_post_meta( get_the_ID(), 'your_custom_field', true );

    if ( $custom_field ) {
        return 'your-custom-image-size';
    }

    return $size;
} );

Let me know if you need more info :)

Thank you Tom
I'm sorry, but I'm not really a technician.
I suppose I have to enter the code you gave me in the functions.php file, right?
What am I suppose to enter in 'your-custom-image-size' ? For example: '1020px'; ?
What kind of custom field should I create? I downloaded the Advanced Custom Fields plugin and the choice is very wide...

No worries!

You can create image sizes like this: https://docs.generatepress.com/article/adding-image-sizes/

Then you would add your image size name into the filter I provided above.

All PHP code should be added using one of these methods: https://docs.generatepress.com/article/adding-php/

As for the custom field, it should just be a standard custom field on your pages. You could make it as easy as a checkbox that you check to enable the larger featured image on that page.

Just make sure you update your_custom_field with the name of the custom field you create.

Thank you very much Tom, I followed your instructions, but doesn't work. I have created a checkbox custom field and added the following code in the snippet plugin:

Snippet 1

add_action( 'init', function() {
add_image_size( 'my-archive-featured-image', 1320, 300, true ); // 500 width, 200 height, crop
} );

Snippet 2

add_filter( 'generate_page_header_default_size', function( $size ) {
$custom_field = get_post_meta( get_the_ID(), 'prova', true );

if ( $custom_field ) {
return 'my-archive-featured-image';
}

return $size;
} );

where I've been wrong?
I have another doubt: I would need the featured image to take the whole size of the container (1320 px) as in the example here but maybe the presence of the right sidebar will prevent it, isn't it?
how would you do it in case?

Maybe I have found another way. I can use different page headers based on featured image dimensions of my article.
Is there a way to assign page header directly from the post options? I would like to let choose the page header before publishing the article....

Excuse me if I add more but going on I thought that maybe the best thing would be to let the user choose to use either a header page or the default solution, with the classic featured image, based on the blog post layout. is it possible?

In this case I would have only 1 problem: having also the featured image in the page header I would have 2 featured images in the same page.
Is there a way not to load the featured image when the Page Header is present? I don't mean css rules like display:none ...

Thank you Tom, and how can I do for letting the writer of a post assign the page header directly from the post options?

This archived topic is closed to new replies.