Add custom size feature image

Add Custom Size Feature Image- WordPress Tutorial

Yes, It’s possible to add custom size feature image in WordPress. In this WordPress tutorial, we are going to add custom size feature image in WordPress.

Create Child Theme

It’s a good idea to make changes via the child theme. It has some benefits as:

  1. Your changes will be forever. If you make changes in the main theme, they will lose on theme update. To make your changes long lasting, it’s better to do with the child theme.
  2. You do not have to change theme’s file. Just copy the template file from the main theme and put it in the child theme. By doing so whenever the template call, it will be called from the child theme directory.

 

How to create Child Theme

WordPress.org proposed some method to create the child theme. You can read them at Create a Child Theme.

The Child theme can create in two ways.

1.  Import parent theme CSS in child theme style.css file.

2. Enqueue parent theme CSS in the child theme functions.php file.

The second method is preferred as there will be the chance to CSS override.

Let’s make it simple for you. Why go through such long method. You can use a free plugin to make the child theme. You can download this plugin at One Click Child Theme.

Please install the plugin to your site. Now go to Dashboard >> Appearance and Select Child Theme menu.

You will See like below screen.

create child theme - Add Custom Size Feature Image- WordPress Tutorial

And Enter Name, Description and author name for the child theme. Click Create Child and your child theme will be created.

add_image_size()– Custom Size Feature Image Function

WordPress provides the custom image size function. This function, add_image_size(), lets you specify image sizes and give you the option to crop.

add_image_size( string $nameint $widthint $heightbool|array $crop = false )

Description

The cropping behaviour for the image size is dependent on the value of $crop:

  1. If false (default), images will be scaled, not cropped.
  2. If an array in the form of array( x_crop_position, y_crop_position ):
    • x_crop_position accepts ‘left’ ‘center’, or ‘right’.
    • y_crop_position accepts ‘top’, ‘center’, or ‘bottom’. Images will be cropped to the specified dimensions within the defined crop area.
  3. If true, images will be cropped to the specified dimensions using centre positions.

Parameters

  • $name
    (string(Required) Image size identifier.
  • $width
    (int(Required) Image width in pixels.
  • $height
    (int(Required) Image height in pixels.
  • $crop
    (bool|array(Optional) Whether to crop images to specified width and height or resize. An array can specify the positioning of the crop area.

Default value: false

Add Custom Size Feature Image Function to the child theme

To add Custom Size Feature Image Function to the child theme please do as

  1. In the dashboard, go to Appearance >> Editor.
  2. You will show screen as below image. Now open functions.php file from right side section.
  3. Please make sure that you are editing the child theme file. You can choose your theme from right section.
  4. Now  Add Custom Size Feature Image Function to mentions place. You have to add code in the last for the functions.php file.
  5. Please make sure that all functions are closed.

Code to Be added is

add_action( 'after_setup_theme', 'custom_thumbnail_size' );
function custom_thumbnail_size() {
add_image_size( 'category-thumb', 300 ); // 300 pixels wide (and unlimited height)
add_image_size( 'homepage-thumb', 220, 180, true ); // (cropped)
}

You can add size as per your requirement.

Show Custom Size Feature Image in your WordPress theme

Now you have added custom size feature image features to the theme. Now build in them with the theme. Open the theme file where you want to display the image and paste the following code:

<?php the_post_thumbnail( 'your-specified-image-size' ); ?>

This code must be pasted inside the post loop.

By following this tutorial you are able to add your own sized feature images.

Let’s show this custom size for all images

Yes, it is possible to add custom size for all images. And you have to do nothing but just add a bit of code in the child theme functions.php.
add_filter( ‘image_size_names_choose’, ‘my_custom_sizes’ );
function my_custom_sizes( $sizes ) {
return array_merge( $sizes, array(
‘homepage-thumb’ => __( ‘Home Page Thumb’ ),
‘category-thumb‘ => __( ‘Category Thumb’ ),
) );
}

 

custom size for all images - Add Custom Size Feature Image- WordPress Tutorial

 

Hope you like this tutorial.

Subscribe to this blog to get latest post direct into your inbox.

Share the Article

About author
Darshan Saroya

Darshan Saroya

A passionate WordPress Theme Developer. I love to create clean and modern themes, using the power of WordPress. Here I provide the tutorials related to WordPress Development, Core PHP and HTML.