- This topic has 23 replies, 4 voices, and was last updated 3 years ago by Tom.
-
AuthorPosts
-
April 20, 2021 at 6:23 pm #1742440ElvinStaffCustomer Support
A part of this code is broken.
add_action( 'init', function() { add_image_size( 'hero-image', 500, 300, true ); add_image_size( 'eventhero-image', 700, 400, ); } );
In
add_image_size( 'eventhero-image', 700, 400, );
the400,
has a,
but you didn’t specify true or false for the cropping parameter.If you don’t want to specify, remove the
,
. So you either do:
add_image_size( 'eventhero-image', 700, 400 );
OR
add_image_size( 'eventhero-image', 700, 400, true );
So it will look like this:
add_action( 'init', function() { add_image_size( 'hero-image', 500, 300, true ); add_image_size( 'eventhero-image', 700, 400, true ); } );
As for the PHP snippet for hero thumbnail:
this should work:
add_filter( 'generate_hero_thumbnail_id_size', function($size) { // Set defaults size if conditions not met $size = 'full'; // Check if post is your_first_post_type if ( is_singular( 'events' ) ) { $size = 'eventhero-image'; } // Else check if is your_other_post_type elseif ( is_singular( 'profiles' ) ) { $size = 'hero-image'; } // Return the thumbnail size return $size; });
I only added the CPT slugs and the image handle names.
April 20, 2021 at 10:46 pm #1742593qpaqAh I see, I corrected the size code. But problem persists; the resized thumbnails are not displayed on posts. This snippet causes the post to display the original uploaded sizes of the images.
// Add new image sizes add_action( 'init', function() { add_image_size( 'hero-image', 500, 300, true ); add_image_size( 'eventhero-image', 700, 400 ); } ); add_filter( 'generate_hero_thumbnail_id_size', function($size) { // Set defaults size if conditions not met $size = 'full'; // Check if post is your_first_post_type if ( is_singular( 'events' ) ) { $size = 'eventhero-image'; } // Else check if is your_other_post_type elseif ( is_singular( 'profiles' ) ) { $size = 'hero-image'; } // Return the thumbnail size return $size; });
April 20, 2021 at 11:27 pm #1742636ElvinStaffCustomer SupportAh I see, I corrected the size code. But problem persists; the resized thumbnails are not displayed on posts. This snippet causes the post to display the original uploaded sizes of the images.
I believe this isn’t retroactive. If you’ve added the image size AFTER adding the image, you’ll have to make WordPress generate these added image sizes.
Can you try regenerating the image sizes to be sure?
https://wordpress.org/plugins/regenerate-thumbnails/April 21, 2021 at 1:55 pm #1743750qpaqYes, I’ve regenerated the thumbnails with that plugin. I have both
hero-image: 500×300 pixels (cropped to fit)
andeventhero-image: 700×400 pixels (proportionally resized to fit inside dimensions)
for each image. Yet the neither theevents
nor theprofiles
CPTs retrieve them.April 21, 2021 at 6:00 pm #1743885ElvinStaffCustomer SupportThat’s quite strange.
Can you link us to a sample post with images for
events
andprofiles
CPT?Can you let us temporarily access the backend as well to check the Header Element? So we could do a closer inspection.
You can use the private information text field for the details. Let us know.
April 21, 2021 at 11:31 pm #1744089qpaqSure, I’ll write it down. Let me know what you’ve changed so I can record it.
April 22, 2021 at 5:05 pm #1745326ElvinStaffCustomer SupportAh I think I see the problem here.
the code was written for a custom post type. Your posts don’t seem to be custom post types but rather, custom taxonomy terms under “Content type”.
You may have to use
is_tax()
rather thanis_singular()
. Do you know the slug of “Content Type”? Let us know.April 22, 2021 at 11:28 pm #1745528qpaqHi Elvin,
Sorry my mistake. Not familiar so much with the terminology.
The Content Type taxonomy is created by PODS.
content_type
is the slug I think you’re asking for.
The two Content Types I would like to differentiate by the size of featured images areevents
andprofiles
I changed to snippet as you suggested, re-generated the thumbnails and purged the cache but the result is the same, problem not resolved.
// Add new image sizes add_action( 'init', function() { add_image_size( 'hero-image', 500, 300, true ); add_image_size( 'eventhero-image', 700, 400 ); } ); add_filter( 'generate_hero_thumbnail_id_size', function($size) { // Set defaults size if conditions not met $size = 'full'; // Check if post is your_first_post_type if ( is_tax ( 'events' ) ) { $size = 'eventhero-image'; } // Else check if is your_other_post_type elseif ( is_tax ( 'profiles' ) ) { $size = 'hero-image'; } // Return the thumbnail size return $size; });
April 23, 2021 at 9:19 am #1746254TomLead DeveloperLead DeveloperHi there,
You may need to ask Pods what condition you need to use. Looking at the body classes of that post,
is_tax()
won’t work, either. There doesn’t seem to be any other clues in the HTML as to which condition to use, unfortunately. -
AuthorPosts
- You must be logged in to reply to this topic.