WordPress featured image link

Today I was working on a portfolio section in a WordPress site and I ran into a problem. The WordPress site was using featured images to show past work from the agency. The featured image was set up to take a large image and then WordPress would resize it to use as a thumbnail. Once you were in the portfolio section of the site, you could click the thumbnail and a lightbox would bring up the larger image for better detail of the agencies work.

The problem came about when the agency wanted to display TV and radio spots in the featured image lightbox. They still wanted to have a featured image, but when the user clicked on the featured image they wanted it to load the TV or radio spot in the lightbox. WordPress doesn’t have a feature that allows you to link a featured image to a post or anything other than the image you uploaded, so I had to come up with a solution.

The Solution

What I ended up doing was using the custom field functionality of WordPress. The logic pretty much said, if there is a custom field use the URL for the featured image, if not use the featured image link.

Here is the code that the WordPress site was using at first for the featured image:


After I created the new custom field, the new code looked like this:

ID, 'Image', true)) ? get_post_meta($post->ID, 'Image', true) : get_thumb_urlfull($post); ?>
 

Now the featured image can be linked to whatever URL is placed in the custom field. The prettyPhoto lightbox that is being used will load any kind of content that is needed. The script can also be modified so the featured image will link to posts or external sites.

Did this WordPress solution work for your project? Leave a comment below and let us know how you used the script or solved this problem. If you have any question or concerns feel free to contact us or use the comment section below.