How to create custom auto generated image sizes in WordPress
Published: 24, Oct 2014
PHPWordPress
If you’ve ever needed to create custom image sizes for your WordPress installation, this function will come in very handy.
This function relies on the WordPress add_image_size() function.
For your own reference the add_image_size() function uses the following parameters:
<?php add_image_size( $name, $width, $height, $crop ); ?>
Simply modify the code below as you see fit.
// Create custom sizes // This is then pulled through to your theme using the_post_thumbnail('custombig'); if ( function_exists( 'add_image_size' ) ) { add_image_size('customsmall', 300, 200, true); //narrow column add_image_size('custombig', 400, 500, true); //wide column }
Set the first parameter of your function to the name of the size you want to reference later in your WordPress theme.
You can use the following example to retrieve the custom image within your own theme / template:
<?php if ( has_post_thumbnail()): // Print out the custom image and also add the class custombig to the image for easy css manipulation the_post_thumbnail( 'custombig', array( 'class' => 'custombig' ) ); endif; ?>