I have a WordPress site which posts have images and text within it. I want to remove the <p>
tag on the images only.
html:
<div class="col-md-12 ">
<?php the_content(); ?>
</div>
This puts each piece into a <p>
tag (images and text)
EDIT – what I attempted
html
<?php
preg_match_all('/(<img [^>]*>)/', get_the_content(), $images);
for( $i=0; isset($images[1]) && $i < count($images[1]); $i++ ) {
echo $images[1][$i];
}
?>
was able to pull the images out but that’s it
No need to reinvent the wheel, CSS Tricks have a great example that is used by quite a few themes and devs:
Just apply this filter in your themes /functions.php