删除Wordpress帖子中图像上的p标签


Remove p tags on images in Wordpress posts

我有一个Wordpress网站,里面有图片和文本。我只想删除图片上的<p>标签。

html:

    <div class="col-md-12 ">
        <?php the_content(); ?>
    </div>  

这会将每个片段放入<p>标签(图像和文本)

编辑-我尝试的

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];
        }
?>  

能够提取图像,但仅此而已

不需要重新发明轮子,CSS技巧有一个很好的例子,被很多主题和开发者使用:

function filter_ptags_on_images($content){
   return preg_replace('/<p>'s*(<a .*>)?'s*(<img .* '/>)'s*(<'/a>)?'s*<'/p>/iU', ''1'2'3', $content);
}
add_filter('the_content', 'filter_ptags_on_images');

只需在您的主题/功能中应用此过滤器即可。php