How to add custom image size with metabox?

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

Leave a comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: