Skip to content

Instantly share code, notes, and snippets.

@natejacobson
Last active August 29, 2015 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save natejacobson/4aa2be3a7b30b577ac07 to your computer and use it in GitHub Desktop.
Save natejacobson/4aa2be3a7b30b577ac07 to your computer and use it in GitHub Desktop.
Normalize WordPress images and figures by auto wrapping images and exposing assigned image classes on figure
function imageWrap() {
$("article img").each( function() {
var img_classes = $(this).attr("class");
var img_src = $(this).attr("src");
if ( !$(this).parent().is("figure") ) {
$(this).unwrap("p").wrap('<figure class="figure-auto figure-back '+img_classes+'" style="background-image: url(\''+img_src+'\')"></figure>');
} else {
$(this).parent("figure").removeAttr("style").addClass("figure-norm figure-back").addClass(img_classes).css("background-image","url('"+img_src+"')");
}
});
}
if ( $("body.single").length || $("body.page").length ) {
imageWrap();
}
<style>
.figure-back { background-image: none !important; }
</style>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment