Black Friday Sale! Get up to $30 off GP Premium! Learn more ➝

[Resolved] Custom Featured Image Size/Class

Home Forums Support [Resolved] Custom Featured Image Size/Class

Home Forums Support Custom Featured Image Size/Class

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #209778
    Jessica

    Trying to add a custom image size in my functions file so when I use the featured image to upload and set the image, it’s showing the FULL size, which I do not want. I need it to automatically upload and hard crop to 948×400.

    In the past sites I’ve created, I have always added this to my functions file as:

    add_image_size(‘featured-image’, 945, 400, true);

    It has to be automatically hard-cropped (on pages and posts) that size and the “header” add-on will unfortunately not work for this site (given the client/end user’s ability to know what they are doing). The consistency and automatic function is necessary in this case.

    I know I can add classes in my style.css to further set this in motion, but there is no indication how the featured image is referred in the site’s core code in order to add the above action with the right name.

    In the end, once deployed, I will not be the only one editing the site & uploading featured images so I need to utilize the featured image area exclusively – and not have any other options than just uploading an image via featured image.

    I’ve tried the above function, and even after regenerating images, nothing works.

    #209806
    Tom
    Lead Developer
    Lead Developer

    Hi Jessica,

    Once the above function is added, you need to tell GP to use that image size instead of the full image.

    For example:

    add_filter( 'generate_page_header_default_size','generate_change_page_header_size' );
    function generate_change_page_header_size()
    {
        return 'featured-image';
    }

    You can also use the Blog add-on to set the exact width and height of your post images in “Customize > Blog > Post Images”.

    Hope this helps 🙂

    #209818
    Jessica

    YESSSSS! Thank you!

    Absolute perfection! Your filter added to my added image size was they key. Magical! THANK YOU!

    #209819
    Tom
    Lead Developer
    Lead Developer
    #238287
    Jamal

    You can also use the Blog add-on to set the exact width and height of your post images in “Customize > Blog > Post Images”.

    Will this apply to CPTs as well? It doesnt seem to work for me.

    What would be the full code in the opening post? Tried several but i keep breaking functions.php file

    Thanks !

    Agressively support the kind of work you want to see. Buy it. Talk about it. Review it.

    #238397
    Tom
    Lead Developer
    Lead Developer

    It should work for all CPTs I believe.

    The code here is the full code: https://generatepress.com/forums/topic/custom-featured-image-sizeclass/#post-209806

    Are you getting a specific error?

    #238438
    Jamal

    I meant this code

    add_image_size(‘featured-image’, 945, 400, true);

    I got some codes from WordPress codex that were making functions.php go blank although through ftp i can see the code is still there and everything returns back to normal after removing those code snippets.

    I will try through the blog add on again and regenerate images this time ( forgot to do that earlier)

    Agressively support the kind of work you want to see. Buy it. Talk about it. Review it.

    #238489
    Tom
    Lead Developer
    Lead Developer

    Those look like curly quotes (thanks WordPress).

    Replace those with straight quotes and it should work:

    add_image_size('featured-image', 945, 400, true);

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