【问题标题】:wordpress - functions.php - preg_replace - <div<-tagswordpress - functions.php - preg_replace - <div<-tags
【发布时间】:2012-12-12 14:30:32
【问题描述】:

我对 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>

目标是用样式属性去掉第二个&lt;div&gt;,去掉&lt;p&gt;&lt;br/&gt;

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

谁能帮帮我?我可以做一些 html 编码,但这远远超出了我的技能。

【问题讨论】:

    标签: php html wordpress function


    【解决方案1】:
    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。

    【讨论】:

      【解决方案2】:

      您是否尝试过使用 jQuery 命令将其删除,例如 jQuery('.entry-content br').remove()

      在 php 级别,您可以使用the_content 过滤器来更改内容。

      【讨论】:

      • 绝对不要为此使用javascript。
      猜你喜欢
      • 1970-01-01
      • 2015-02-05
      • 2017-03-04
      • 2018-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-24
      • 1970-01-01
      相关资源
      最近更新 更多