Get all images from a page or a post within the loop (2 posts)

  1. Cedric
    Posted 6 years ago #


    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:

    $customQuery = new WP_Query('page_id=XX');
    while ( $customQuery->have_posts() ) : $customQuery->the_post();

    <div id="images">
    $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); }
    <?php endwhile; ?>

  2. Micah Cooksey

    Posted 6 years ago #

    You might try Get the image.

Topic Closed

This topic has been closed to new replies.

About this Topic