首页 文章

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

提问于
浏览
1

我有一个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];
        }
?>

能够拉出图像,但就是这样

1 回答

  • 1

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

    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');
    

    只需在主题/functions.php中应用此过滤器即可

相关问题