Site logo

Archived topic

Featured image in the Header Element

23 replies · Started by qpaq on October 5, 2020

Viewing posts 16–24 of 24

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, ); the 400, 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.

Ah 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;
});

Ah 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/

Yes, I've regenerated the thumbnails with that plugin. I have both hero-image: 500×300 pixels (cropped to fit) and eventhero-image: 700×400 pixels (proportionally resized to fit inside dimensions) for each image. Yet the neither the events nor the profiles CPTs retrieve them.

That's quite strange.

Can you link us to a sample post with images for events and profiles 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.

Sure, I'll write it down. Let me know what you've changed so I can record it.

Ah 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 than is_singular(). Do you know the slug of "Content Type"? Let us know.

Hi 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 are events and profiles

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;
});

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

This archived topic is closed to new replies.