WordPress.org

Ready to get started?Download WordPress

Forums

wp_get_attachment_image not showing image (4 posts)

  1. gatessg
    Member
    Posted 8 months ago #

    I want to move the first image attached to a post out of the loop and into a different div separated from the text that is associated with the post. After reading different articles online it looked like <?php echo wp_get_attachment_image( ); ?> is the best way to go about doing this?

    I have added <?php echo wp_get_attachment_image( 2 ); ?> into my theme outside the loop to try and pull the second image associated with a post. However this is not doing anything.

    Any suggestions are appreciated. Thanks in advance.

  2. alchymyth
    The Sweeper & Moderator
    Posted 8 months ago #

    please review http://codex.wordpress.org/Function_Reference/wp_get_attachment_image

    the first parameter for the function is the attachment ID

  3. gatessg
    Member
    Posted 8 months ago #

    Thanks alchymyth, I was reading this for a while but I'm not really as good with php as I would like to be yet. I'm confused if this is able to be used outside the loop or not. As I specified before it doesn't seem to be responding at all.

  4. gatessg
    Member
    Posted 8 months ago #

    I found this solution to work outside the loop.

    ` <?php $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>';
    }
    } ?>
    `
    I feel that this my be a little over kill for what I'm trying to do. This works but I am wondering if I really need all this or if it is becoming sloppy.

Reply

You must log in to post.

About this Topic