WordPress has many predefined functions for important features. The same goes with the get attachment image function. wp_get_attachment_image(); function is related to thumbnail image. This function is not just limited to blog posts. It can be used with custom post type or even with a particular page.
There are situations when we need to show a thumbnail or we can say image on a template in WordPress. It can be inside a post page or custom post types. wp_get_post_attachment(); is used in this type of situations.
How to use wp_get_attachment_image(); ?
Don’t get overwhelmed by the name, It is pretty simple to use. If you know something about the other WordPress functions related to thumbnail. Then you can understand this one easily. Even if you don’t know anything or never heard about something like this. No worries, I will teach everything to you. I have given an example so you can understand better.
wp_get_attachment_image ( $attachment_id, string|array $size = 'thumbnail', bool $icon = false, string|array $attr = '' )
If you look at the example above. You can see how the parameters are used within the function. In the first parameter, the ID of the attachment is specified. Then the size of the attachment is specified(can be pre-registered or set). Finally, the boolean value for the $icon parameter is specified.
It checks whether the attachment is an image or not. If the attachment is an image, then it returns with the specific size of the image. For other instance, the function returns with an icon. If the $icon value is set to true. The boolean value is set to false by default. WordPress uses a function to get the attachment dynamically.
Here is an example.
get_posts( array( 'post_type' => 'attachment' ) ), etc.
What are the parameters accepted by get_attachment_image(); ?
wp_get_attachment_image( $attachment_id, $size, $icon, $attr );
The above-mentioned example uses all the parameters related to the function. Below I have explained about these parameters in detail. The function accepts three parameters.
- $attachment_id – It is the ID of the attachment that we want to display.
- $size(optional) -It is basically image size. It accepts the registered size of that image. If the registered size is not there, an array is passed with width and height in pixels. It’s always better to register size with $size parameter. It generates cropped version.
- $icon(optional)– It performs a check to know whether to treat the attachment as an icon or not. To do this it should be set to true.
- $attr(optional)– It is an array of attributes that needs to be passed with the element.
get_attachment_image(); to fetch the image from custom options echo wp_get_attachment_image( get_theme_mod( $option_id_in_string ) );
It is also used to fetch images from custom options. For example, you want to fetch images from themes/plugins. Which was uploaded from custom options. Just copy the code that I have shown below.
echo wp_get_attachment_image( get_theme_mod( $option_id_in_string ) );
That’s all for this article. We covered everything about get_attachment_image(); function. WordPress has some other functions that are very similar to this one.
1. the_post_thumbnail : this function is used to display the thumbnail image in the front-end.
2. get_the_post_thumbnail : this function is similar to the one above. But the difference is that it returns with a container. The container contains the thumbnail image.
3. get_attachment_image_url etc. : This works the same as get_attachment_image();. The difference is that it doesn’t return with element. Instead, it returns with the URL of the image.
Although you are going to use these functions more often. If we compare them to get attachment thumbnail. If you find is helpful in any way. Please check our other useful articles as well. If you have any query related to this, you can reach us any time. Thank you for reading the post.