Wordpress modify the_content markup
With jQuery or PHP, I would like to modify the dom structure of the the_content function in WordPress. In some posts I use the h3 element, and I would like to add a wrapper that contains the cont开发者_运维知识库ent until the next h3.
So I would like to convert this:
<h3>Title</h3>
<p>This is just regular text</p>
<h3>Next title</h3>
Into this:
<div class='wrapper'>
<h3>Title</h3>
<p>This is just regular text</p>
</div>
<div class='wrapper'>
<h3>Next title</h3>
</div>
Thanks!
assuming that the content only consists of <h3>s and <p>s,and they are welled formated like:
<div id="content">
<h3>title</h3>
<p>..........</p>
<p>..........</p>
<h3>another title</h3>
<p>.........</p>
<h3>yet another title</h3>
<p>..........</p>
</div>
then you may try this in jQuery.
//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>');
精彩评论