My goal is to strip the images and iframes out of WordPress posts (they are in .para divs) and move them to a <ul>
above the post, just inside the local wrapper (.wrapper940).
The jQuery below works, but moves ALL image and iframes from all posts to a single new <ul>
above the first post. Instead of this, I am trying to move the images and iframes PER POST to a new <ul>
just inside the wrapper of each post.
Here's the jQuery:
jQuery("document").ready (function($){
var matchesEl = $(".para img, .para iframe");
if ( matchesEl.length > 0) {
var newUl = $("<ul></ul>");
newUl.prependTo(matchesEl.parents(".wrapper940"));
matchesEl.each(function() {
var newLi = $("<li></li>").append($(this)).appendTo(newUl);
});
}
});
The html is:
<div class="news-item-wrap">
<div class="date">the date</div>
<div class="wrapper940">
<div class="title">the title</div>
<div class="para">
<p>The main content of the post.</p>
<p>Which could be several paragraphs</p>
<p>And include iframes...</p>
<p><iframe src="//www.youtube.com/embed/uGMbZNTym-g" width="560" height="315" frameborder="0" allowfullscreen="allowfullscreen">...</iframe>
</p>
<p>Followed by more text... and maybe some images....</p>
<p><a href="http://www.joujouka.org/wp-content/uploads/2014/05/festival-interculture.jpg"><img class="alignnone size-medium wp-image-404" alt="festival intercultural" src="http://www.joujouka.org/wp-content/uploads/2014/05/festival-interculture-213x300.jpg" width="213" height="300"/></a>
</p>
</div>
</div>
<div class="news-item-wrap">
<div class="date">the date</div>
<div class="wrapper940">
<div class="title">the title</div>
<div class="para">
<p>A second post would follow like this.</p>
<p>Which could also be several paragraphs</p>
<p>And include iframes...</p>
<p><iframe src="//www.youtube.com/embed/uGMbZNTym-g" width="560" height="315" frameborder="0" allowfullscreen="allowfullscreen">...</iframe>
</p>
<p>Followed by more text... and maybe some images....</p>
<p><a href="http://www.joujouka.org/wp-content/uploads/2014/05/festival-interculture.jpg"><img class="alignnone size-medium wp-image-404" alt="festival intercultural" src="http://www.joujouka.org/wp-content/uploads/2014/05/festival-interculture-213x300.jpg" width="213" height="300"/></a>
</p>
</div>
</div>
This would continue for as many posts as there were. So I need to be able to move the images and iframes FOR EACH POST to appear just inside the .wrapper940 that wraps EACH POST. (i.e. above the title of each post.)
I think that using .parents()
is sending all images and iframes from all posts to the first .wrapper940
; .closest()
seems like it should work, but doesn't, maybe because it breaks the loop?
Any help very much appreciated!