Here is the procedure for adding custom image size on metabox/metafield of post

I hope you all are familiar with after_setup_theme theme hook. Here i first defined image size using add_image_size function:
example:
//adding function_exists() before function will let user to customize the same function on child theme

if ( !function_exists( ‘theme_slug_image_size_for_metabox’)) :
function theme_slug_image_size_for_metabox(){

//set thumbnail image for recent post
add_image_size ( ‘themeslug-thumbnail-recent’, 80,100,true);
}
endif;
add_action(‘after_setup_theme’, ‘theme_slug_image_size_for_metabox’);

step2:

if( get_post_meta( $post->ID, ‘id_of_metabox’, true)) {
$featured_images = get_post_meta( $post->ID, ‘id_of_metabox’, true);

foreach ($featured_images as $key => $featured_image) {
//her variable $key hold the attachement id of image.
$img_src = wp_get_attachment_image_src($key,’themeslug-thumbnail-recent’, false); ?>

<img src=”<?php echo $img_src[0]; ?>” alt=”” title=””>// This will display image with custom image size

<?php }

}

 

 

 

 

 

Advertisements