This is a variant on another question brilliantly answered here.
I need to use jQuery to move WordPress post attachments - images and iframes - to a new div outside of the post itself, and to do this on a per-post basis (so that each post's images and iframes stay local to it, within the local post wrapper.
My current code almost accomplishes this, but moves all of the images and iframes from ALL posts into one new div in the first post.
Here's the jQuery:
jQuery("document").ready (function($){
var matchesEl = $("section img, section iframe");
if ( matchesEl.length > 0) {
var newDiv = $("<div />", {'class': 'royalSlider'});
newDiv.prependTo(matchesEl.parents("section"));
matchesEl.each(function() {
$(this).addClass('rsImg').appendTo(newDiv);
});
}
});
And here's the html before the jQuery works on it:
<section class="active">
<p>
<a href="<a href="http://www.freshorigins.com/wp-content/uploads/2013/05/good21-copy.jpg"><img class="alignnone size-full wp-image-61" src="http://www.freshorigins.com/wp-content/uploads/2013/05/good21-copy.jpg" alt="gallery_1286896812" width="980" height="651"></a>"></a>
<a href="<a href="http://mediapresentations4u.com/wp-content/uploads/2014/06/flower-delivery-online.jpg"><img class="alignnone size-full wp-image-61" src="http://mediapresentations4u.com/wp-content/uploads/2014/06/flower-delivery-online.jpg" alt="gallery_1286896812" width="980" height="651"></a>"></a>
</p>
</section>
<section>
<p>
<a href="<a href="http://www.freshorigins.com/wp-content/uploads/2013/05/good21-copy.jpg"><img class="alignnone size-full wp-image-61" src="http://www.freshorigins.com/wp-content/uploads/2013/05/good21-copy.jpg" alt="gallery_1286896812" width="980" height="651"></a>"></a>
<a href="<a href="http://mediapresentations4u.com/wp-content/uploads/2014/06/flower-delivery-online.jpg"><img class="alignnone size-full wp-image-61" src="http://mediapresentations4u.com/wp-content/uploads/2014/06/flower-delivery-online.jpg" alt="gallery_1286896812" width="980" height="651"></a>"></a>
</p>
</section>
There would be several more <sections>
.
This works, but it takes the images and iframes from all <sections>
and moves them into the first <section>
.