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