I am using this code to get the url of first inserted image in a post. Is there any way I can get the ID of that image.

      global $post, $posts;
      $first_img = '';
      $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
      $first_image = $matches [1] [0];
  • 1
    on default image should have a class with attachment id like attachment-{id}. Its the only way i can think of. – Sisir Aug 2 '12 at 18:25

No, an image placed inside a post/page content don't reference in your database other than inside your content only.

That's why an image attached is better: you have full control over your images since they are in your server and referenced in your database. An attachment is a child post, once you attach an file to a post/page you can use get_children for retrieve file description, title etc.

  • Thanks .... by the way to whoever down-voted it would be helpful to maybe mention why? – byronyasgur Aug 2 '12 at 20:14

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.