Web Development

Get All Attached Images in WordPress

Now a days, wordpress is a great platform for blogging and for even CMS also. Also we can customize the wordpress site with very easy and effective ways. This is due to code functionality and flexibility provided by the wordpress team.

As I have mentioned that we can customize this easily. So here is the one of the portion which we can customize using just in-built wordpress functions.

In this article we will see how we can get all attached image to any post. Wordpess has one function [code]get_children()[/code] with which we can achieve this functionality very easily.

Have a look at below code block for the same.

[cc lang=”php”]
$all_images = get_children(
array(
‘post_type’ => ‘attachment’,
‘post_mime_type’ => ‘image’,
‘post_parent’ => $post->ID
));

var_dump($all_images);

[/cc]

In above code block [code]$post->ID[/code] is the post id of which you want to get all attached images. Here you can see that we have filtered the attachments with mime type so we can get images only.

Hope this trick will be useful to you when you are dealing with these type of customisation part.

Shares:

Leave a Reply

Your email address will not be published. Required fields are marked *