Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

Jose Morales-Mendizabal
Jose Morales-Mendizabal
19,175 Points

Adding "Title" HTML attributes to <a> tags that wrap the thumbnails inserted via Wordpress Media Uploader

Hi everyone,

I'm currently developing a Wordpress website that utilizes a Lightbox Gallery plugin. The plugin works light any other lightbox: Each thumbnail image is wrapped by a link that links to the full-size image which is shown on an overlay upon clicking the thumbnail. This plugin also comes with a caption functionality by overlaying the text within the Title HTML attribute found on the link wrapping the thumbnail.

I know that on Wordpress you can set the Title Atrribute from the Media Library, however, this Title attribute is only set on the <img> Thumbnail nested within the link, and not the link itself. I need the Anchor tag to have this attribute and so I came up with a client side solution that uses jQuery to iterate through each thumbnail, get its Title Attribute, traverse up to its parent anchor tag and set it its Title Attribute to the one fetched.

I guess my question is : is this client side solution the best way to go about solving this problem? Or would it be better to do something via PHP, perhaps using the functions.php on Wordpress? I've included the jQuery code below so you can see the code I'm using to solve the problem

$('#links img').each(function(){
// iterate through each thumbnail and store its title Attr in a variable
    var thumbnail = $(this);
    var imageTitle = thumbnail.attr('title');

// Use the thumbnails cached attribute and set it as the parent's title attribute
    thumbnail.parent('a').attr('title', imageTitle);


});

2 Answers

Jose Morales-Mendizabal
Jose Morales-Mendizabal
19,175 Points

Hey Zac! Thanks for the tip, I will check that course in the coming days. However, was the solution I implemented a bad practice? It seems to be working very well, and since it's just 5 lines of code, It is super performant.