Site logo

Archived topic

Changing the featured image size of some post categories

7 replies · Started by Oliver on March 13, 2021

Viewing posts 1–8 of 8

I would like to remove the featured image shown inside the blog post (not in the blog roll) for some categories but not for others. The reason is that many of my posts have book covers as their featured images and they look off when viewing on desktop.

I don´t know if that is possible but I managed to exclude posts from the blog roll using the code below and so I thought one might be able to do the same but excluding categories to show featured image inside the post


add_filter('pre_get_posts', 'excludeCat');
function excludeCat($query) {
      if ( $query->is_home ) {
            $query->set('cat', '-1, -12, -18');
      }
      return $query;
}

Hi there,

you can use the option_generate_blog_settings filter:

https://docs.generatepress.com/article/option_generate_blog_settings/

For example:

add_filter( 'option_generate_blog_settings', 'db_remove_category_specific_featured_images' );
function db_remove_category_specific_featured_images( $options ) {
	if ( in_category( array( 'category one', 'category two' ) ) ) {
		$options[‘page_post_image’] = false;
	}
  
  return $options;
}

David, thanks for the reply

I pasted your code in a new snippet. I tried editing the code as follow:

Changing the category number to the actual number of the categories I want to stop showing the featured image inside the posts, but it didn´t work. I then tried replacing 'category one' and so on, for the actual category names (slug names) but with no luck either.
Should I have edited the code in another way?

Hi Elvin,
I tried it again and it didn´t work. I must be doing something wrong but I can´t figure out what it is.

This is what I´m writing into the code snippet:


add_filter( 'option_generate_blog_settings', 'db_remove_category_specific_featured_images' );
function db_remove_category_specific_featured_images( $options ) {
	if ( in_category( array( '11', '14' ) ) ) {
		$options[‘page_post_image’] = false;
	}
  
  return $options;
}

where 11 and 14 are two category ids i wish not to show their featured image inside the blog post.

Amazing!
Thank you very much
Cheers!

You're welcome

This archived topic is closed to new replies.