首页 文章

删除未使用的p标签

提问于
浏览
0

我有以下代码将<p>标记放回我的Wordpress帖子中的段落 . (我删除了图像并将它们放在后面,这只是为了放置文本)

<p>
<?php
    $content = preg_replace('/(<img [^>]*>)/', '', get_the_content());
    $content = wpautop($content); // Add paragraph-tags
    $content = str_replace('<p></p>', '', $content); // remove empty paragraphs
    echo $content;
?>
</p>

它工作,并再次将我的段落放在p标签中,但它在我的实际段落之间输出空<p> </ p>标签 . 我想摆脱这些空段落标签

2 回答

  • -1

    你只是覆盖变量

    试试这个(注意 .= 而不是 =.= 添加到变量而不是覆盖变量 .

    $content = preg_replace('/(<img [^>]*>)/', '', get_the_content());
    $content .= wpautop($content); // Add paragraph-tags
    $content .= str_replace('<p></p>', '', $content); // remove empty paragraphs
    echo $content;
    
  • -1

    使用此功能,它将帮助您删除此 <p></p> 标记

    <?php echo get_the_content();?>
    

相关问题