WordPress get all image sizes by ID

Featured images have become an important part of WordPress since they were first released with WordPress 2.9. Some articles and tutorials might also refer to them as post thumbnails, but they mean the same thing.

If you are unfamiliar with featured images, read the linked tutorial to quickly learn all the basics of featured images. While the linked article provides an overview of featured images for beginners, this tutorial will cover the programming aspect.

This tutorial will show you how to get and set the size of featured images in WordPress. If you want to learn how to get the featured image or its ID for a particular post and check if a post even has a featured image, you should read my post titled How to Get the Featured Image in WordPress.

Understanding Various Image Sizes in WordPress

Images form an important part of websites and help in making a page more lively and interesting. However, they can also add a considerable amount of extra data that users need to download to view a page. Things can get even worse when you are using a single size of an image to display on all devices, large and small.

Luckily, WordPress automates the creation of images of different sizes for you so that you can optimize the content delivery for different viewers. I have covered this topic in detail in the tutorial about changing featured image sizes in WordPress.

In short, WordPress has multiple image sizes and one such default image size is called Thumbnail. The dimensions for this image size are available from the WordPress admin dashboard by navigating to Settings > Media.

The default Thumbnail image size is different from another image size called post-thumbnail, which gets registered whenever a theme adds support for featured images.

The focus of discussion in this tutorial is going to be the post-thumbnail size, and we will learn how to get and set its value.

You will most likely want the size of your featured images to be consistent. One easy way to achieve this is with the help of the

1
50 function. The function accepts three parameters: width, height, and an optional boolean value specifying whether you want to crop the images or resize them. This is set to
1
51 by default, which means that images will be resized.

The following line instructs WordPress to create image thumbnails that are 1200 px wide and 628 px tall with resizing.

1
set_post_thumbnail_size(1200, 628);

Keep in mind that images will be resized to be exactly 1200 px and 628 px wide only if they have the same aspect ratio. Otherwise, they will be resized to either have a matching width or a matching height, depending again on the aspect ratio.

Let's say that you don't care about the images getting cropped, but they always need to be 1200 px wide and 628 px tall when used as featured images. In that case, you can set the third parameter to be

1
52.

1
set_post_thumbnail_size(1200, 628, true);

All your featured images will now be cropped to be exactly 1200 px wide and 628 px tall.

The

1
50 function uses the
1
54 function behind the scenes to register an image size for the post thumbnail. If you want to register any additional image sizes, you should consider using the
1
54 function.

Adding this function call to your functions.php file will not result in resizing of any existing featured images. You will have to use a thumbnail regeneration plugin to achieve that. Also, a smaller image will not be upscaled to the featured image dimensions that you specified.

Let's say you want to find out the currently registered size for featured images to see if it is what you expect. How do you do that?

WordPress offers us multiple functions to get all this information. I will mention them here briefly.

The

1
56 function is useful for anyone who just wants a list of different registered image sizes. This won't give you any other information about the image sizes. Here is its output for my website:

1
Array
2
(
3
    [0] => thumbnail
set_post_thumbnail_size(1200, 628);
0
set_post_thumbnail_size(1200, 628);
1
set_post_thumbnail_size(1200, 628);
2
set_post_thumbnail_size(1200, 628);
3
set_post_thumbnail_size(1200, 628);
4
set_post_thumbnail_size(1200, 628);
5
set_post_thumbnail_size(1200, 628);
6
set_post_thumbnail_size(1200, 628);
7
set_post_thumbnail_size(1200, 628);
8
set_post_thumbnail_size(1200, 628);
9
1
0
1
1
1
2
1
3
1
4
1
5
1
6
1
7
1
8
1
9

The

1
57 function shows you information related to all other registered image sizes besides thumbnail, medium, medium_large, and large. This function returns an associative array of arrays, with information such as the width, height, and cropping for the registered image sizes. Here is the output of this function for me:

1
Array
2
(
3
set_post_thumbnail_size(1200, 628, true);
5
set_post_thumbnail_size(1200, 628);
0
set_post_thumbnail_size(1200, 628, true);
7
set_post_thumbnail_size(1200, 628);
2
set_post_thumbnail_size(1200, 628, true);
9
set_post_thumbnail_size(1200, 628);
4
1
1
set_post_thumbnail_size(1200, 628);
6
1
3
set_post_thumbnail_size(1200, 628);
8
1
5
1
0
1
2
1
8
1
4
set_post_thumbnail_size(1200, 628, true);
7
1
6
Array
2
1
8
Array
4
Array
5
1
3
Array
7
1
5
Array
9
2
0
2
1
2
2
set_post_thumbnail_size(1200, 628, true);
7
2
4
2
5
2
6
2
7
2
8
2
9
(
0
1
5
(
2
(
3
(
4
(
5
set_post_thumbnail_size(1200, 628, true);
7
(
7
(
8
(
9
3
0
3
1
2
9
3
3
1
5
3
5
3
6
3
7
3
8
set_post_thumbnail_size(1200, 628, true);
7
    [0] => thumbnail
0
    [0] => thumbnail
1
    [0] => thumbnail
2
    [0] => thumbnail
3
    [0] => thumbnail
4
    [0] => thumbnail
5
    [0] => thumbnail
6
1
5
    [0] => thumbnail
8
    [0] => thumbnail
9
set_post_thumbnail_size(1200, 628);
00
set_post_thumbnail_size(1200, 628);
01
set_post_thumbnail_size(1200, 628, true);
7
set_post_thumbnail_size(1200, 628);
03
set_post_thumbnail_size(1200, 628);
04
set_post_thumbnail_size(1200, 628);
05
set_post_thumbnail_size(1200, 628);
06
set_post_thumbnail_size(1200, 628);
07
2
9
set_post_thumbnail_size(1200, 628);
09
1
5
set_post_thumbnail_size(1200, 628);
11
set_post_thumbnail_size(1200, 628);
12
1
9

You can see the

1
58 size that I registered in the above output. It also has
1
59 set to
1
60, which means that the thumbnails will be cropped instead of resized.

Let's say you want information about all the registered image sizes for a WordPress website. The

1
61 function is your best bet in that case. Here is my output with a call to this function:

1
Array
2
(
3
set_post_thumbnail_size(1200, 628);
19
set_post_thumbnail_size(1200, 628);
0
set_post_thumbnail_size(1200, 628, true);
7
set_post_thumbnail_size(1200, 628);
2
set_post_thumbnail_size(1200, 628);
23
set_post_thumbnail_size(1200, 628);
4
set_post_thumbnail_size(1200, 628);
25
set_post_thumbnail_size(1200, 628);
6
1
3
set_post_thumbnail_size(1200, 628);
8
1
5
1
0
1
2
set_post_thumbnail_size(1200, 628);
32
1
4
set_post_thumbnail_size(1200, 628, true);
7
1
6
set_post_thumbnail_size(1200, 628);
36
1
8
set_post_thumbnail_size(1200, 628);
38
Array
5
1
3
Array
7
1
5
Array
9
2
0
set_post_thumbnail_size(1200, 628);
45
2
2
set_post_thumbnail_size(1200, 628, true);
7
2
4
set_post_thumbnail_size(1200, 628);
49
2
6
    [0] => thumbnail
3
2
8
1
3
(
0
1
5
(
2
(
3
set_post_thumbnail_size(1200, 628);
58
(
5
set_post_thumbnail_size(1200, 628, true);
7
(
7
set_post_thumbnail_size(1200, 628);
62
(
9
set_post_thumbnail_size(1200, 628);
64
3
1
1
3
3
3
1
5
3
5
3
6
set_post_thumbnail_size(1200, 628, true);
5
3
8
set_post_thumbnail_size(1200, 628, true);
7
    [0] => thumbnail
0
set_post_thumbnail_size(1200, 628, true);
9
    [0] => thumbnail
2
1
1
    [0] => thumbnail
4
1
3
    [0] => thumbnail
6
1
5
    [0] => thumbnail
8
    [0] => thumbnail
9
1
8
set_post_thumbnail_size(1200, 628);
01
set_post_thumbnail_size(1200, 628, true);
7
set_post_thumbnail_size(1200, 628);
03
Array
2
set_post_thumbnail_size(1200, 628);
05
Array
4
set_post_thumbnail_size(1200, 628);
07
1
3
set_post_thumbnail_size(1200, 628);
09
1
5
set_post_thumbnail_size(1200, 628);
11
set_post_thumbnail_size(1200, 628);
12
2
1
set_post_thumbnail_size(1200, 628);
98
set_post_thumbnail_size(1200, 628, true);
7
1
00
2
5
1
02
2
7
1
04
2
9
1
06
1
5
1
08
1
09
(
4
1
11
set_post_thumbnail_size(1200, 628, true);
7
1
13
(
8
1
15
3
0
1
17
2
9
1
19
1
5
1
21
1
22
3
7
1
24
set_post_thumbnail_size(1200, 628, true);
7
1
26
    [0] => thumbnail
1
1
28
    [0] => thumbnail
3
1
30
1
3
1
32
1
5
1
34
1
35
set_post_thumbnail_size(1200, 628);
00
1
37
set_post_thumbnail_size(1200, 628, true);
7
1
39
set_post_thumbnail_size(1200, 628);
04
1
41
set_post_thumbnail_size(1200, 628);
06
1
43
2
9
1
45
1
5
1
47
1
48
1
9

This gives you all the information that you might need about registered image sizes in WordPress, including the featured image size signified by the

1
58 key.

Some theme developers might want to style featured images differently than regular images. WordPress makes it incredibly easy for us to differentiate between the two with CSS selectors.

Any featured image that you output on the website will have a class named

1
63 already applied to it. The image will also have some other optional classes added to it, like
1
64  or
1
65, depending on the size that you requested for the featured image.

This allows you to target featured images of a specific size and apply styles accordingly.

Final Thoughts

This tutorial covered everything that you need to know about getting or setting the size of featured images in any WordPress installation. You should now be able to specify exactly what size you want your featured images to be and then style those images based on the specific classes applied to them.

How do I get an image from an ID in WordPress?

Usage. wp_get_attachment_image( $attachment_id, $size, $icon, $attr ); If the attachment is an image, the function returns an image at the specified size. For other attachments, the function returns a media icon if the $icon parameter is set to true.

How do I show full size images in WordPress?

To do this navigate to a post or page you want to add the image and click the '+' icon to add a new block. Then click the 'Image' icon to add an image block where you can paste your URL. Click 'Insert from URL', then paste your full size image URL into the box and press the 'Enter'.

What is the list of thumbnail sizes in WordPress?

Thumbnail size: 150 x150 pixels. Medium size: 300 x 300 pixels. Medium-Large size: 724 x auto pixels. Large size: 1024 x 1024 pixels.
Yes, you can change the number of featured images displayed in WordPress. There are two ways to do this. Go to Settings > Media > Featured Image size and enter the desired image size. Then, click "Save changes."