I'm trying to show only content with all paragraphs (possibly divs,...) but with no attached images.

I had some success with:

echo preg_replace('/<img[^>]+\>/i', '',get_the_content());

But it removes paragraphs not just images.. Any idea why?

  • Before I posted I made research but as I said - this preg_replace also removes <p> – HunterListInsight Nov 24 '14 at 18:03
  • No, it does not. Did you actually tested the accepted answer from the duplicate. The code you are using also does not remove p tags, p tags has not yet being added – Pieter Goosen Nov 24 '14 at 18:18

Your function is not removing the p tags: they have not yet been added as get_the_content returns the unfiltered content. You can manually add the p tags using wpautop.

$unfiltered = preg_replace('/<img[^>]+\>/i', '',get_the_content());
$filtered = wpautop($unfiltered);

echo $filtered;
|improve this answer|||||

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