Display Images from Posts or Pages in WordPress Theme Template

25
Feb

This code snippet demonstrates how to display images from Posts or Pages in your WordPress Theme Template


To use this code snippet it must be placed in your ‘single’ or ‘category’ theme page(s).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?php
$attachment_args = array(
    'post_type'        => 'attachment'
    'order'               => 'ASC',
    'post_parent'      => $post-&gt;ID,
    'post_mime_type' => 'image',
    'post_status'       => null
);
$attachments = get_posts($attachment_args);
if ($attachments) {
    foreach ($attachments as $attachment) {
        echo apply_filters('the_title', $attachment->post_title);
        echo '<img src="'.wp_get_attachment_url($attachment->ID, 'thumbnail', false, false).'" />
    }
}
?>
<?php
$attachment_args = array(
    'post_type'        => 'attachment'
    'order'               => 'ASC',
    'post_parent'      => $post-&gt;ID,
    'post_mime_type' => 'image',
    'post_status'       => null
);
$attachments = get_posts($attachment_args);
if ($attachments) {
    foreach ($attachments as $attachment) {
        echo apply_filters('the_title', $attachment->post_title);
        echo '<img src="'.wp_get_attachment_url($attachment->ID, 'thumbnail', false, false).'" />
    }
}
?>

Be sure to check out our other great WordPress Code Snippets and WordPress Articles.

Enjoy this article? If so, we would love to hear your thoughts in the comments below

 

Related Blog Posts

Leave a Reply