Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't get add_image_size to work

Tags:

wordpress

I'm creating a WordPress theme and am trying to create various different sized images for the images the user uploads into an Advanced Custom Fields image field. However when I use the code below then use the Regenerate Thumbnails plugin to create the new image sizes it doesn't appear to have any affect.

I get the feeling there's something very basic I'm missing as from everything I've read this should work. The code below is a cut down version of the actual function, there's a fair few more image sizes in the one I'm using.

function test_add_image_sizes() {
  add_theme_support( 'post-thumbnails' );

  // Images used on blog index & category pages for the first item and at the top of the blog post page itself
  add_image_size( "main-image", 700, 680, true );
  add_image_size( "main-image-350", 350, 450, true ); // For use on screens 350 pixels & below
  add_image_size( "main-image-539", 539, 573, true ); // For use on screens 539 pixels & below
  add_image_size( "main-image-659", 659, 570, true ); // For use on screens 659 pixels & below
  add_image_size( "main-image-850", 850, 650, true ); // For use on screens 850 pixels & below
  add_image_size( "main-image-1000", 1000, 650, true ); // For use on screens 1000 pixels & below
  add_image_size( "main-image-1200", 1200, 680, true ); // For use on screens 1200 pixels & below
  add_image_size( "main-image-1400", 1400, 680, true ); // For use on screens 1200 pixels & above
}
add_action( 'init', 'test_add_image_sizes' );

The code above is placed in my themes functions.php file.

like image 321
Neil Nand Avatar asked Nov 08 '22 07:11

Neil Nand


1 Answers

Hi use after_theme_setup hook instead of init. User regenerate thumbnail plugin for previously uploaded image size fixing.

function test_add_image_sizes() {
  add_theme_support( 'post-thumbnails' );

  // Images used on blog index & category pages for the first item and at the top of the blog post page itself
  add_image_size( "main-image", 700, 680, true );
  add_image_size( "main-image-350", 350, 450, true ); // For use on screens 350 pixels & below
  add_image_size( "main-image-539", 539, 573, true ); // For use on screens 539 pixels & below
  add_image_size( "main-image-659", 659, 570, true ); // For use on screens 659 pixels & below
  add_image_size( "main-image-850", 850, 650, true ); // For use on screens 850 pixels & below
  add_image_size( "main-image-1000", 1000, 650, true ); // For use on screens 1000 pixels & below
  add_image_size( "main-image-1200", 1200, 680, true ); // For use on screens 1200 pixels & below
  add_image_size( "main-image-1400", 1400, 680, true ); // For use on screens 1200 pixels & above
}
add_action( 'after_theme_setup', 'test_add_image_sizes' );
like image 121
Faysal Haque Avatar answered Nov 15 '22 13:11

Faysal Haque