php - wordpress - functions.php - preg_replace - <div<-tags

标签 php html wordpress function

我对 wordpress 的帖子有疑问。使用在wordpress上自动发布的外部博客的代码嵌入功能,html代码弄乱了我的样式。

我有以下代码:

    <div class="entry-content">
     <div style="width:450px;margin:0 auto">
      <div style="position:relative;">
       <a target="_blank" href="DIFFERENT VARIABLES">
        <img width="???" alt="DIFFERENT VARIABLES" src="DIFFERENT VARIABLES" title="DIFFERENT VARIABLES" height="???" />
       </a>
      </div>
     </div>
     <p><br/>
     <div style="text-align:center"><small>SEVERAL <a>-TAGS</small></div>
    </div>

目标是摆脱第二个<div>使用样式属性并摆脱 <p><br/> .

最好的方法是为 function.php 添加一个函数,因为我确实有很多帖子。

谁能帮帮我?我足以编写一些 html 代码,但这远远超出了我的技能范围。

最佳答案

function my_function($content) {
    $div = '<div style="width:450px;margin:0 auto">';
    // Make sure the offending div is there.
    if (strpos($content, $div) !== false) {
         // Remove div.
         $content = str_replace($div, '', $content);
         // Remove one ending div tag.
         $content = str_replace('</div>', '', $content, 1);
         // Remove the p, br.
         $content = str_replace('<p><br/>', '', $content, 1);
    }
    return $content
}
add_filter('the_content', 'my_function');

将此添加到您的 functions.php。

关于php - wordpress - functions.php - preg_replace - <div<-tags,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13841883/

相关文章:

php - 如何在 Laravel 中将变量传递给另一个 Controller

PHP: simple_html_dom - 如何查找不在某个类中的元素?

php - 当 ID 未知时保存到表

php - 使用 Behat/Selenium 在特定的 DIV 中查找内容

wordpress - 我可以通过 xmlrpc 删除未附加的媒体吗?

html - 使同级 div 不要将其兄弟推到左边太远

javascript - 当 chrome 中的嵌套滚动中的数据更改时防止页面滚动

html - rails 发送多封电子邮件?

html - WooCommerce:显示没有描述的产品的简码?

javascript - 粒子 slider Logo 不会在 Safari 中调整大小