Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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 = '';
      ob_start();
      ob_end_clean();
      $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
      $first_image = $matches [1] [0];
share|improve this question
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
add comment

1 Answer

up vote 0 down vote accepted

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.

share|improve this answer
    
Thanks .... by the way to whoever down-voted it would be helpful to maybe mention why? –  byronyasgur Aug 2 '12 at 20:14
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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