How to get all image attachment post IDs linked to a WP post for which I have the post ID with me

Hi

How do I get all image attachment IDs that are linked to a Post. I have the Post ID with me

Thanks,

Arvind

  • Jack Kitterhing
    • Code Norris

    Hi there @arvind

    What do you want to do with the images, how do you want to output them?

    <?php if ( have_posts() ) : while ( have_posts() ) : the_post();

    $args = array(
    'post_type' => 'attachment',
    'numberposts' => -1,
    'post_status' => null,
    'post_parent' => $post->ID
    );

    $attachments = get_posts( $args );
    if ( $attachments ) {
    foreach ( $attachments as $attachment ) {
    echo '<li>';
    echo wp_get_attachment_image( $attachment->ID, 'full' );
    echo '<p>';
    echo apply_filters( 'the_title', $attachment->post_title );
    echo '</p></li>';
    }
    }

    endwhile; endif; ?>

    That would display and images and titles attached, if that’s what your looking for?

    Thanks!

    Kind Regards

    Jack.

  • Jack Kitterhing
    • Code Norris

    Hi there @arvind

    What do you want to do with the images, how do you want to output them?

    <?php if ( have_posts() ) : while ( have_posts() ) : the_post();

    $args = array(
    'post_type' => 'attachment',
    'numberposts' => -1,
    'post_status' => null,
    'post_parent' => $post->ID
    );

    $attachments = get_posts( $args );
    if ( $attachments ) {
    foreach ( $attachments as $attachment ) {
    echo '<li>';
    echo wp_get_attachment_image( $attachment->ID, 'full' );
    echo '<p>';
    echo apply_filters( 'the_title', $attachment->post_title );
    echo '</p></li>';
    }
    }

    endwhile; endif; ?>

    That would display and images and titles attached, if that’s what your looking for?

    Thanks!

    Kind Regards

    Jack.

  • Jack Kitterhing
    • Code Norris

    Hi again @arvind

    Or you might be looking for this

    <?php
    $args = array( 'post_type' => 'attachment', 'posts_per_page' => -1, 'post_status' =>'any', '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 );
    }
    }
    ?>

    ? http://codex.wordpress.org/Template_Tags/get_posts#Show_attachments_for_the_current_post

    Thank you!

    Kind Regards

    Jack.

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.