Automatically Get Images From Posts/Page In WordPress Using PHP

Here’s a nice and quick WordPress trick you might want to try out on your site. It’s a hack that will let you get attached images from a post or page and then output them. This is a nice little piece of code in cases where you want to collate certain images and use them for a gallery page or to show just a thumbnail gallery.

Just copy/paste the code below into your functions.php file:

$args = array(
'post_type' => 'attachment',
'post_mime_type' => 'image',
'post_parent' => $post->ID
);
$images = get_posts( $args );
foreach($images as $image):
echo wp_get_attachment_image($image->ID, 'medium');
 endforeach;

It’s may be an old and classic trick but it’s nonetheless a topic or question that still keeps being brought up in blogs and forums. Hope this helps!

Incoming search terms for the article:

Related Posts

Creating Instagram Filters with Photoshop

Add Breadcrumbs Without A Plugin In WordPress

Redirecting Users To Bypass WordPress Dashboard

Twitter Feed Reader Using PHP

1 Comment

  1. Joe Cain

    08.04.2012

    your coding is very need ful for me! I was used it.