Linking gallery images to a sponsor

Some customers are asking me on how to link the gallery images to a sponsor (affiliate program URL). Actually this is very easy, but requires a slight modification of your WordPress theme.

Let’s consider that CyberSEO plugin pulls some sponsor FHG feed or parses a CSV dump with links to image galleries. The plugin generates WordPress posts with galleries and puts your affiliate link into the “paysite” custom field.

So if you want to link every full-sized gallery image (not a small thumbnail) to your affiliate link, you should open your attachment.php template and find there a line like this:

<a href="<?php echo wp_get_attachment_url(); ?>"><img src="<?php echo wp_get_attachment_url(); ?>" alt="<?php the_title_attribute(); ?>" /></a>

now just modify it as follows and save the template:

<a href="<?php echo get_post_meta(wp_get_post_parent_id(get_the_ID()), "paysite", true); ?>" target="_blank" rel="nofollow"><img src="<?php echo wp_get_attachment_url(); ?>" alt="<?php the_title_attribute(); ?>" /></a>

Vualà: now all your gallery images are linked to your sponsor’s site with your own affiliate code of course ;)

If you want to link the gallery images to some static URL (http://www.somesite.com/), you should do it like this:

<a href="http://www.somesite.com/" target="_blank" rel="nofollow"><img src="<?php echo wp_get_attachment_url(); ?>" alt="<?php the_title_attribute(); ?>" /></a>