I enjoy simplicity, in everything. Unnecessary complexity is evil. Have a look at my WordPress plugins.

Adding an affiliate link to your excerpts.

Are you an affiliate who’s running a review site on WordPress and do you want to tweak your CTR a little? You’ve come to the right place, in this article i’ll explain to you how to add a direct link to your excerpt that points to the the product/service you’re reviewing. This gives your visitors the option to choose between reading on or visit the seller’s site immediately, and most likely saves some visitors one extra click.

For some this article will be somewhat difficult, but if you’ve got some basic knowledge of HTML and PHP you should be fine. If you’re used to playing around in your theme’s files you’ll definetly be able to do this. Basically everything you need is provided by me, you’ll just need to copy, paste and configure it to your likings. I’ll try to make it as easy for you as possible!

The end result..

I know that it’s a little weird to start with the end result, but I want you to see how this will look in the end. Nothing gives you a better idea then a screenshot, so watch this.

Backend Affiliate Link

Now, that’s what it will look like in the backend, but what will it look like for your visitors? After all, that’s what matters. Well, have a look yourself!

The frontend look of the affiliate link

Where do we start?

So you liked the end result and decided that you want to add this to your blog? Great! Let’s get started. Basically what we want is to add a little piece of data to every review, which holds the affiliate link. Since we want to enter this link in the panel where we write the review, we’re going to add a metabox there.

Adding the MetaBox

To add the metabox we’re going to use a great PHP class called WPAlchemy, created by Dimas Begunoff. It lets you easily add as much metaboxes as you want, and limit them to certain post types, categories, post or pages. Go over there and download the WPAlchemy package. You’ll find that it contains two folders, extract those to your theme folder.

Now, open your functions.php and include the WPAlchemy MetaBox class as followed.

include_once TEMPLATEPATH . '/WPAlchemy/MetaBox.php';
// include css to help style our custom meta boxes
// this should be a global stylesheet used by all similar meta boxes
if (is_admin()) wp_enqueue_style('custom_meta_css',TEMPLATEPATH.'/custom/meta.css');

Now we can start to use this class to add our own custom metaboxes. For this we’re going to create a new object, which we can configure to our likings.

$affiliate_link = new WPAlchemy_MetaBox(array
	'id' => '_affiliate_link_info', // underscore prefix hides fields from the custom fields area
	'title' => 'Affiliate link options',
	'template' => TEMPLATEPATH . '/custom/affiliate_link.php',
	'types' => 'post',
	'include_category' => 'Reviews'

Basically this just adds a metabox to all the posts in the category Reviews. The HTML code for this metabox is grabbed from the file ‘affiliate_link.php‘ in your theme’s subfolder called ‘custom‘.

Setting up the metabox fields.

Now we’re going to create the file called ‘affiliate_info.php‘ in which we set-up our metabox fields. In the above example I used just two fields, but you can of course set up as many as you like.

<div class="my_meta_control">
	<p>Here you can add a affiliate link to this article, that will be shown after the excerpt on archive pages..</p>
		<?php $metabox->the_field('linktitel'); ?>
		<input type="text" name="<?php $metabox->the_name(); ?>" value="<?php $metabox->the_value(); ?>"/>
		<span>The anchor text of the link, if not given, the title of the post will be used.</span>
		<?php $metabox->the_field('link'); ?>
		<input type="text" name="<?php $metabox->the_name(); ?>" value="<?php $metabox->the_value(); ?>"/>
		<span>The affiliate link (url) itself. (e.g. http://theproduct/?ref=me)</span>

When you added all you’re input fields, save the file and close it. Have a look in your backend to see if you did everything right, and if you did you should have some sweet extra metaboxes for posts in the Review category.

Calling the affiliate links at your archive pages

Now you can store the affiliate links as extra information to your review posts, but we still don’t use them. That’s why we’re going to create a custom template for your ‘Reviews’ category pages. Beneath each excerpt we want to show two options to the visitor, read on or visit immediately.

If you’re category slug is ‘reviews’, what you should do is create a new file called ‘category-reviews.php‘. This will tell WordPress to use that file as a template for the category ‘Reviews’ page. More information about Template Hierarchy can be found at the WordPress Codex.

Now, open up your themes ‘archive.php‘ file, and copy all of it’s content to your newly created ‘category-reviews.php‘. Now you have the basic structure of your category page, just like all the other category pages. If done right you should have a file looking somewhat like this, as a minimum basic..

<?php get_header(); ?>
<div id="content">
<h1><?php single_cat_title(); ?></h1>
 <?php if (have_posts()): while (have_posts()) : the_post(); ?>
 <div class="bericht">
  <a href="<?php the_permalink(); ?>">
 <?php qoate_att_image(0, 'thumbnail', 'class="alignright" height="85" alt="' . $post->post_title . '"'); ?>
	<h3 class="berichttitel"><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h3>
	<?php the_excerpt(); ?>
	<?php endwhile; else :?>
	<p>Sorry, we couldn't find any posts in this category yet.. </p>
	<?php endif; ?>
<div id="sidebar">
<?php get_sidebar(); ?>
<?php get_footer(); ?>

Adding the read more to the end of the excerpt.

The read more link at the end of the excerpt in above example is just added by adding a filter to the excerpt. You can just copy the following if you want the exact same result.

function new_excerpt_more($post) {
	return '...<span class="readmore">&amp;raquo; Read more about <a href="'. get_permalink($post->ID) . '">' . get_the_title($post->ID) . '</a></span>';
add_filter('excerpt_more', 'new_excerpt_more');

Notice that I have my read more text in a span element? This is to style it to my liking. The minimum styling is to add a display:block to it. This will make sure it’s get treated as a block element, and thus appear on a new line.

Adding the affiliate link

To acces the affiliate link we have to acces our stored info for that post. With WPAlchemy this is done quite easy. Because you’ll probably have more then one review excerpt on your category page we’re going to create a function for this, so that we can easily call that from our template file.

Open up your functions.php and add the following function.

function show_affiliate_link(){
global$post; global $affiliate_link;
echo '<a rel="nofollow" target="_blank" href="';
echo '">';
if(strlen($affiliate_link->get_the_value('linktitel')) > 0) { $affiliate_link->the_value('linktitel'); }
else { the_title(); }
echo '</a>';

This calls the stored affiliate link, and adds a rel=”nofollow” and target=”_blank” to it, which you probably want to have for your affiliate links. Notice that we check for the length of the given title, if it’s not given we will use the post’s title as an anchor text for the link.

Calling the function from your Reviews template

Now we’re almost there! The only thing that’s left is to actually call the function from our Reviews category template, so that it will add the link to each excerpt. Just add the following line of code just beneath ‘<?php the_excerpt(); ?>‘.

<span class="readmore">&raquo; Direct link to  <?php show_affiliate_link(); ?>.</span>

We wrap this line in the same class as the read more text, so it will look the same and appear on a new line. This will add the affiliate link with a little text before it. Go check your Reviews category page and see if you like the output. If not, have fun tweaking around!

Really like this, but think it’s to hard for you?

Did you make it to the end of this article without even thinking of implementing this because you’ll think it will be to hard for you? Just leave a comment! If there are enough people who actually like to see something like this in a plugin, i’ll make it into one!

Share this post: on Twitter on Facebook

3 Responses to “Adding an affiliate link to your excerpts.”

    • Hi Janelle,

      Sorry for the late reply. Didn’t make a plugin out of this yet since the functionality is pretty theme specific.

      Note: Since this post is pretty old and WP Alchemy went through some changes I don’t think this exact code snippet will work.

Leave a Reply