• This is one of those that drive me crazy.

    Below is the code I’m using to get the image (jpg) from a post. Every post has just one image. The code below is living in single.php and the return value is the last image existing in attachments.

    The problem is; If I go to another post it will still returns the last image in attachemnts. It seems that it is not recognizing the post_parent value since it is giving me the last image of all images existing in each post.

    Please, help me out if you have some experience with this functions. Thank you,

    <?php

    //echo “id ” . the_ID();

    $arrImages =& get_children( ‘post_parent=’ . the_ID() . ‘&post_type=attachment&numberposts=1&post_mime_type=image’ );

    //echo “in”;

    if ( empty($arrImages) )
    {
    $attachment_url = “#”;
    }
    else
    {
    foreach ( (array) $arrImages as $arrImage )
    {
    //$attachment_url =& wp_get_attachment_url( $arrImages->ID );
    //$attachment_url = $arrImages[0];

    $attachment_url = wp_get_attachment_url( $arrImage->ID );
    echo $attachment_url;
    }

    }

    ?>

Viewing 2 replies - 1 through 2 (of 2 total)
  • I had a similar problem, it occured when i used images from the media library, it worked for me by uploading the image and adding it directly to the post.

    I think what you’re looking for is

    array_reverse($arrImages)

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘How to get url of image in post’ is closed to new replies.