- This topic has 7 replies, 3 voices, and was last updated 7 years, 6 months ago by Tom.
-
AuthorPosts
-
July 14, 2016 at 3:38 pm #209778Jessica
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.
July 14, 2016 at 5:26 pm #209806TomLead DeveloperLead DeveloperHi 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 š
July 14, 2016 at 5:54 pm #209818JessicaYESSSSS! Thank you!
Absolute perfection! Your filter added to my added image size was they key. Magical! THANK YOU!
July 14, 2016 at 5:59 pm #209819TomLead DeveloperLead DeveloperYou’re welcome š
October 24, 2016 at 4:08 am #238287JamalYou 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 !
October 24, 2016 at 10:06 am #238397TomLead DeveloperLead DeveloperIt 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?
October 24, 2016 at 11:13 am #238438JamalI 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)
October 24, 2016 at 2:05 pm #238489TomLead DeveloperLead DeveloperThose look like curly quotes (thanks WordPress).
Replace those with straight quotes and it should work:
add_image_size('featured-image', 945, 400, true);
-
AuthorPosts
- You must be logged in to reply to this topic.