如何解决WordPress修改the_content标记
| 使用jQuery或PHP,我想修改WordPress中the_content函数的dom结构。在某些帖子中,我使用了h3元素,并且我想添加一个包含内容的包装,直到下一个h3。 所以我想将其转换为:<h3>Title</h3>
<p>This is just regular text</p>
<h3>Next title</h3>
变成这个:
<div class=\'wrapper\'>
<h3>Title</h3>
<p>This is just regular text</p>
</div>
<div class=\'wrapper\'>
<h3>Next title</h3>
</div>
谢谢!
解决方法
假设内容仅由
和
组成,并且它们的格式如下:
那么您可以在jQuery中尝试。
<div id=\"content\">
<h3>title</h3>
<p>..........</p>
<p>..........</p>
<h3>another title</h3>
<p>.........</p>
<h3>yet another title</h3>
<p>..........</p>
</div>
//get the main post content
$content=$(\"#content\");
$h3s=$content.find(\'h3\');
$h3s.each(function(index){
if(index==0)$(this).before(\'<div class=\"wrapper\">\');
else $(this).before(\'</div><div class=\"wrapper\">\');
});
//remeber to close the last one
$content.append(\'</div>\');
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。