Get All Attached Images in WordPress

  • Avinash
  • 1
  • Feb 20, 2012
  • Web Development

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 get_children() with which we can achieve this functionality very easily.

Have a look at below code block for the same.

$all_images = get_children( array( 'post_type' => 'attachment', 'post_mime_type' => 'image', 'post_parent' => $post->ID )); var_dump($all_images);
  $all_images = get_children(
    array(
    'post_type' => 'attachment',
    'post_mime_type' => 'image',
    'post_parent' => $post->ID
    ));

  var_dump($all_images);

In above code block $post->ID 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.

Related Posts

Written by Avinash

Avinash Zala is leading various projects which deals with the various technology involved with the web. A combination of perfect technical and management skills. Avinash would like to chat with you and convert your imagination into the working system. You can get in touch with him on Facebook and Twitter.

View all posts by: