[Support request] How to create post template with different featured image size?

Home Forums Support How to create post template with different featured image size?

Viewing 15 posts - 1 through 15 (of 16 total)
  • Author
    Posts
  • #1356738
    Evenit

    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

    #1357261
    Tom
    Lead Developer
    Lead Developer

    Hi there,

    You can use a filter like this: https://docs.generatepress.com/article/generate_page_header_default_size/

    What do you mean by post templates? Are they custom templates you’ve added to your child theme?

    #1357409
    Evenit

    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.

    #1357927
    David
    Staff
    Customer Support

    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.

    #1358662
    Evenit

    It is purely down to individual choice

    #1359369
    Tom
    Lead Developer
    Lead Developer

    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?

    #1361599
    Evenit

    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

    #1362208
    Tom
    Lead Developer
    Lead Developer

    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 🙂

    #1363298
    Evenit

    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…

    #1363589
    Tom
    Lead Developer
    Lead Developer

    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.

    #1363836
    Evenit

    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?

    #1363956
    Evenit

    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….

    #1364000
    Evenit

    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 …

    #1364537
    Tom
    Lead Developer
    Lead Developer

    If you’re creating a Header Element, you can tell it to disable the standard featured image: https://docs.generatepress.com/article/header-element-overview/

    #1364635
    Evenit

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

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