WordPress, Lightbox Plus ColorBox, native galleries and captions

This post is for geeks who use WordPress. I apologise if you now have nothing to read. Maybe give the Bible a try?

Last night, I posted my first ever native WordPress gallery (old dog, new tricks, etc). The thumbnails were pretty enough, but clicking through to a clunky WordPress “attachment page” – or worse, a raw JPEG file – was too much hard work, so I went looking for a Lightbox plugin for WordPress.

Enter Lightbox Plus ColorBox. Install, check the “Use for WordPress Galleries” option, enable Fancypants styling, done!

Well, almost. It turns out ColorBox doesn’t play nice with captions as rendered by WordPress. I needed to insert this above the wp_footer() call in the theme:

<script type="text/javascript">
jQuery(document).ready(function($){
    $(".gallery img").each(function(){
        var alt = $(this).attr("alt");
        $(this).parent().attr("title",function(i,val){
            return val == alt ? "" : alt;
        });
    });
});
</script>

It replaces image titles with their respective captions, unless the caption and title are the same, which would suggest no caption has been specified.

You’re welcome.

5 thoughts on “WordPress, Lightbox Plus ColorBox, native galleries and captions”

Comments are closed.