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

I have the following code:

//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.

  • did you ever figure this out?
    – Starfs
    Jun 26 '13 at 17:52

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;
  • It didn't see to work. No post appears on tumblr. Jun 13 '11 at 13:07
  • i have no idea about tumblr and that wasn't the question, my answer addresses the question "how to get the attachment ID
    – Bainternet
    Jun 13 '11 at 18:09

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.


If the image in question is attached to the post, then you can get the URL of the image using the wp_get_attachment_url function. (Read more in the codex here)

  • That function requires the attachment id also, which is the thing I am struggling to find. Jun 13 '11 at 13:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.