Wordpress Image Captions in wp-lightbox2
November 20th, 2007In an ideal world I would have found a Wordpress image captions plugin that just worked the way I wanted - not a perfect world I guess. Last night I struggled with Javascript and edited wp-lightbox2 so it would display any image with a link having a title attribute as a caption. This is lightweight and works great for me. here is an example:
Here is how I did it:
After downloading and activating wp-lightbox open plugins/wp-lightbox/js/lightbox.js near line 350 after:
if(anchor.getAttribute('href') && (relAttribute.toLowerCase().match('lightbox'))){
anchor.onclick = function () {myLightbox.start(this); return false;}
Add:
//set the id attreibute
anchor.setAttribute('id', 'caption'+i);
//get the caption attribute
captionDiv = document.getElementById('caption'+i);
caption = captionDiv.title;
anchor.innerHTML+='<span class="caption">'+caption+'</span>';
Thats it! Use the class ‘caption’ in your stylesheet to edit the caption’s style.
May 9th, 2008 at 12:02 pm
Thanks for this. But you didn’t actually do what you say you did.
“Last night I struggled with Javascript and edited wp-lightbox2 so it would display any image’s alt attribute as a caption. This is lightweight and works great for me.”
Your script doesn’t use the “alt” attribute from the image. It uses the “title” attribute from the link. If someone follows your directions, it doesn’t work at all.
So you figured it out, came up with a great solution and then explained it wrong on your site.
That’s the only reason I can think of that you haven’t gotten more comments. No one can get it to work.
Well, it’s working for me now. So I’ll thank you again. You might want to touch up your instructions though.
May 10th, 2008 at 7:44 pm
@John
Thanks for catching that. I have fixed my explanation.