1

I am using custom image sizes with add_image_size for custom post types.

For example for books custom post type

if($post_type_name == 'book'){
    add_image_size('75x75',75,75, true);
    add_image_size('150x150',150,150, true);
}elseif($post_type_name == 'music'){
   add_image_size('200x200',200,200, true);
   add_image_size('400x400',400,400, true);
}

But when I regenerate thumbnails it doesn't work. Why?

1

add_image_size is intended to be defined globally (outside of the context of a post type). This is because when images are generated they are not yet attached to a specific post type.

If you're using a plugin or WP CLI to regenerate the thumbnails, you are outside of the context of a post type. Therefore those checks are always going to fail.

You should simply do

add_image_size('75x75',75,75, true);
add_image_size('150x150',150,150, true);
add_image_size('200x200',200,200, true);
add_image_size('400x400',400,400, true);

In your functions.php, class file, plugin, etc.

  • Thank you but I have a lot of image sizes for different post types. How can I do it on different way? – Abuzer Killibacak Jun 2 '17 at 1:04
  • @AbuzerKillibacak In that case you may find this post helpful wordpress.stackexchange.com/questions/1463/… It's not possible to define custom image sizes for custom post types (natively within WordPress) however there are a couple of plugins that can do the job. – Jared Cobb Jun 2 '17 at 1:05
  • It is too complicated for me. I have basic English. And I saved them in different folder for example /upload/book/ or /upload/music/ . Can I do it? Do you have any suggestion? – Abuzer Killibacak Jun 2 '17 at 1:12
  • I'm sorry, I'm sure the language barrier is an additional challenge. I recommend my original answer. Go ahead and generate all image sizes for all post types. Storage is inexpensive. CPU cycles and RAM are expensive. Go ahead and generate the images for all post types, it'll be fine. Your Custom Post Type templates can simply request the proper images sizes. – Jared Cobb Jun 2 '17 at 1:24
  • I did like your original answer. But firstly I added image sizes for my custom post type. Then I go media and I chose my images for that custom post type then I regenerate them. Then I did that for other post types. Thank you. – Abuzer Killibacak Jun 2 '17 at 1:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.