Created
April 13, 2017 15:48
-
-
Save gabelloyd/56df05ba0c2da2940a113ec9670fc46f to your computer and use it in GitHub Desktop.
Make WP Captions Responsive to image widths.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function caption_responsive(){ | |
//// Make captions responsive | |
// we want to change the width of the figcaptions within figures | |
$('.wp-caption').each(function(){ | |
var caption = $(this), | |
figcaption = caption.find('figcaption').first(), | |
img = caption.find('img[class*="wp-image-"]').first(); | |
if (img.length) { | |
// wait for the image to load or it might have no dimensions | |
img.load(function() { | |
// Clear the default WP Core styling | |
caption.removeAttr('style'); | |
figcaption.css("width", img.width()) | |
}); | |
} | |
}); | |
} | |
jQuery(document).ready(function($) { | |
caption_responsive(); | |
$(window).resize(function() { | |
caption_responsive(); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment