How to find attachment ID for first image in a post

I am using a plugin to forward photo posts from wordpress to a tumblr blog.

I have the following code:

Read More
//post blog to tumblr
function postBlogTumblr($postID)
{
    $URLServer = "http://www.tumblr.com/api/write";
    $t_post = get_post($postID);
    $t_url = get_permalink($postID);
    $tumblr_data = unserialize(get_option("tumblr"));
    $postdata['email'] = $tumblr_data['tumblr_login_email'];
    $postdata['password'] = $tumblr_data['tumblr_login_pass'];
    $postdata['type'] = "photo";

    $postdata['source'] = the_attachment_link($attachment_id);

    $postdata['caption'] = $t_post->post_title."(via adamblanchard.co.uk)";   
    $postdata['state'] = "published";
    $postdata = http_build_query($postdata);   
    $result = datapost($URLServer,$postdata);

}

I believe I am using the right method on the $postdata[‘source’] line, but I am unsure how to go about getting the attachment id.

Any guidance would be greatly appreciated.

Related posts

Leave a Reply

3 comments

  1. you can use this snippet to get the first image of a post attachment id:

    $images =& get_children( 'post_type=attachment&post_mime_type=image&post_parent=' . $postID );
    $attachment_id = $images[0]->ID;
    
  2. this small code give you the first image from the post, if is inside the gallery of the post and is! the first image in the gallery of the post.

    $attachments = get_children( array(
                    'post_parent'    => get_the_ID(),
                    'post_type'      => 'attachment',
                    'numberposts'    => 1, // show all -1
                    'post_status'    => 'inherit',
                    'post_mime_type' => 'image',
                    'order'          => 'ASC',
                    'orderby'        => 'menu_order ASC'
                    ) );
    foreach ( $attachments as $attachment_id => $attachment ) {
        echo wp_get_attachment_image( $attachment_id );
    }
    

    Play with my post about this possibilies and you find your best soltution.