Get all images from a page or a post within the loop
-
Hi,
I’m trying to get all the images attached to a post or a page (in my case a page) with a custom query.
I searched all over the web and these forums, tried different things with get_children(), get_post(), etc. but didn’t manage to make it work.
If you’ve already done that, what code did you use?
One of the piece of code that I tried, but unfortunately didn’t work for me:
<?php
$customQuery = new WP_Query(‘page_id=XX’);
while ( $customQuery->have_posts() ) : $customQuery->the_post();
?><div id=”images”>
<?php
$args = array(
‘post_type’ => ‘attachment’,
‘numberposts’ => null,
‘post_status’ => null,
‘post_parent’ => $post->ID);
$attachments = get_posts($args);
if ($attachments) {
foreach ($attachments as $attachment) {
echo apply_filters(‘the_title’, $attachment->post_title); the_attachment_link($attachment->ID, false); }
}
?>
</div>
<?php endwhile; ?>
- The topic ‘Get all images from a page or a post within the loop’ is closed to new replies.