1

I try to get all images out of the content-area of a post. I already tried this

$imgs = get_posts(array(
    'post_type' => 'attachment',
    'post_per_page' => -1,
    'post_parent' => $thread->ID
));

But this only returns the featured image / the post thumbnail.

I also have tried

$imgs = get_post_galleries_images($thread->ID);
foreach($imgs as $img){
    echo $img;
}

But this returns nothing.

Any idea how I can get the images out of the content?

Thanks!

2

You can use DOMDocument class

$doc = new DOMDocument();
$doc->loadHTML($post->post_content);
$images = $document->getElementsByTagName('img');
  • Thanks! That's great :) First time I heard about this functionality! After your code, I looped through the array and got access to the src-attribut via $img->getAttribute("src") – Brotzka Nov 19 '15 at 15:28

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.