php - How to set the post_thumbnail size to specific category or tag?

832

I'm trying to create an post gallery grid with different thumbnail sizes. I want to show a different thumbnail size for posts per category. I've added thumbnail sizes in functions.php and tried to conditionally populate the post-thumbnail with custom sizes with the following code:

if ( function_exists( 'add_theme_support' ) ) {
add_theme_support( 'post-thumbnails' );
add_image_size( 'thumbvert', 500, 750, true );
add_image_size( 'smallvert', 675, 1013, true );}

if ( in_category( 'thumbvert' )) {
set_post_thumbnail('thumbvert');
} elseif ( in_category( 'smallvert' )) {
set_post_thumbnail('smallvert');
} else {
set_post_thumbnail_size( 500, 333 ); }

The thumbnails don't change.

638

Answer

Solution:

Going purely on what you have supplied. You'll need to get the current category name if on an archive page, then run your conditional. Ex:

if ( function_exists( 'add_theme_support' ) ) {
  add_theme_support( 'post-thumbnails' );
  add_image_size( 'thumbvert', 500, 750, true );
  add_image_size( 'smallvert', 675, 1013, true );
}

if ( is_archive() ) {
  $current_category = single_cat_title("", false);
  if ( in_category( $current_category ) ) {
    set_post_thumbnail('thumbvert');
  } elseif ( in_category( $current_category )) {
    set_post_thumbnail('smallvert');
  } else {
    set_post_thumbnail_size( 500, 333 ); 
  }
}

People are also looking for solutions to the problem: jquery - Remove / disable selected option[selected from first dropdown] from second dropdown on clicking add button php

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.