Internal Linking Optimization of WordPress Blog Posts Without Plugins


The term of Internal Linking Optimization means the placement of internal links from one pages of blog or website to another for transferring some more weight to most important pages of your blog. In fact, before making some external WordPress SEO optimization steps, you need to make good internal optimization and internal linking structure is one of the first steps you need to do.

I suppose that if you’re using WordPress, then your website is a blog and the main content of all blogs are blog-posts. This means that you need to give more weight to your posts. If you will have more links, internal and incoming links – you will get more traffic from search engines, especially from Google (read my previous article about How to add WordPress blog to Google). Usually all bloggers and webmasters are using widgets and plugins for internal linking (such as “Popular posts“, “Relative Posts” etc), but this is not enough, because some of the posts may not get in to this lists of links. In this case you need something like “Previous posts” WordPress hack.

Internal Linking Optimization of WordPress Blog Posts Without Plugins

“Ring Chain” internal linking structure

This WordPress hack will help you to create optimized ring “chain” internal linking structure of all your blog posts where absolutely all posts will be interlinked with each other without any plugins.

This is a scheme by which all your posts will be linked:
Internal Linking Optimization of WordPress Blog Posts Without Plugins

As you see on the scheme, all your WordPress posts will linked one by one and the last post will have link to the first post. For example, if you have 10 posts – first post will have 5 links to Post № 2,3,4,5,6 … second post will have links to Post № 3,4,5,6,7 … post № 6 will have links to Post № 7,8,9,10 and 1 – you will have ring of links, where all posts will have same number of incoming internal links. This method is really good for internal SEO optimization of your blog.

How to do this WordPress hack without a plugin

To show list of previous posts of your WordPress blog, you need to past the following code to your functions.php file (just in case, make a copy of functions.php):

<?php
/**
 * Show list with previous posts.
 *
 * by Webdesignzo.com
 */
function webdesignzo_previous_posts ($post_num=5, $format = '', $list_tag='li', $echo=true){
	global $post, $wpdb;
	$sql ="SELECT ID, post_title, post_date, comment_count, guid
	FROM $wpdb->posts p
	WHERE ID < $post->ID
		AND post_type = 'post'
		AND post_status = 'publish'
	ORDER BY ID DESC
	LIMIT $post_num";
	$res = $wpdb->get_results($sql);
		$exclude = $post->ID;
		foreach ($res as $id) $exclude .= ','.$id->ID;
		$count_res = count($res);

	if ( !$res || $count_res<$post_num ){
		$post_num = $post_num-$count_res;
		$sql2 ="SELECT ID, post_title, post_date, comment_count, guid
		FROM $wpdb->posts p
		WHERE ID NOT IN ($exclude)
			AND post_type = 'post'
			AND post_status = 'publish'
		ORDER BY ID DESC
		LIMIT $post_num";
		$res2 = $wpdb->get_results($sql2);
		$res = array_merge($res,$res2);
	}
	if (!$res) return false;
	preg_match ('!\{date:(.*?)\}!',$format,$date_m);
	foreach ($res as $pst){
		$x == 'li1' ? $x = 'li2' : $x = 'li1';
		$Title = $pst->post_title;
		$a1 = "<a href='". get_permalink($pst->ID) ."' title='{$Title}'>";
		$a2 = "</a>";

		if ($format){
			$date = apply_filters('the_time', mysql2date($date_m[1],$pst->post_date));
			$Sformat = str_replace ($date_m[0], $date, $format);
			$Sformat = str_replace('{title}', $Title, $Sformat);
			$Sformat = str_replace('{a}', $a1, $Sformat);
			$Sformat = str_replace('{/a}', $a2, $Sformat);
			$Sformat = str_replace('{comments}', $pst->comment_count, $Sformat);
		}
		else $Sformat = $a1.$Title.$a2;
		$out .= "\n<$list_tag class='$x'>{$Sformat}</$list_tag>";
	}
	if ($echo) echo $out;
	else return $out;
}
?>

Update:

Your function.php file can be different (depends on your WordPress theme). If the last symbols of your function.php file are “?>“, then click right after ?>” and hit “Enter” to create a new line and put my code there, like on the screenshot below (click to enlarge):

If the last symbols of your function.php are different (not ended with “?>“), then you should put my code in new line, but without < ?php at the beginning and ? > at the end... just use the code below instead of what I posted earlier:
/**
 * Show list with previous posts.
 *
 * by Webdesignzo.com
 */
function webdesignzo_previous_posts ($post_num=5, $format = '', $list_tag='li', $echo=true){
	global $post, $wpdb;
	$sql ="SELECT ID, post_title, post_date, comment_count, guid
	FROM $wpdb->posts p
	WHERE ID < $post->ID
		AND post_type = 'post'
		AND post_status = 'publish'
	ORDER BY ID DESC
	LIMIT $post_num";
	$res = $wpdb->get_results($sql);
		$exclude = $post->ID;
		foreach ($res as $id) $exclude .= ','.$id->ID;
		$count_res = count($res);

	if ( !$res || $count_res<$post_num ){
		$post_num = $post_num-$count_res;
		$sql2 ="SELECT ID, post_title, post_date, comment_count, guid
		FROM $wpdb->posts p
		WHERE ID NOT IN ($exclude)
			AND post_type = 'post'
			AND post_status = 'publish'
		ORDER BY ID DESC
		LIMIT $post_num";
		$res2 = $wpdb->get_results($sql2);
		$res = array_merge($res,$res2);
	}
	if (!$res) return false;
	preg_match ('!\{date:(.*?)\}!',$format,$date_m);
	foreach ($res as $pst){
		$x == 'li1' ? $x = 'li2' : $x = 'li1';
		$Title = $pst->post_title;
		$a1 = "<a href='". get_permalink($pst->ID) ."' title='{$Title}'>";
		$a2 = "</a>";

		if ($format){
			$date = apply_filters('the_time', mysql2date($date_m[1],$pst->post_date));
			$Sformat = str_replace ($date_m[0], $date, $format);
			$Sformat = str_replace('{title}', $Title, $Sformat);
			$Sformat = str_replace('{a}', $a1, $Sformat);
			$Sformat = str_replace('{/a}', $a2, $Sformat);
			$Sformat = str_replace('{comments}', $pst->comment_count, $Sformat);
		}
		else $Sformat = $a1.$Title.$a2;
		$out .= "\n<$list_tag class='$x'>{$Sformat}</$list_tag>";
	}
	if ($echo) echo $out;
	else return $out;
}

Save your functions.php file.

Now, to show list of previous posts, you just need to put next code into single.php (it can be different template file, it depends on your WordPress theme):

<ul>
<h3>Previous posts:</h3>
<?php webdesignzo_previous_posts (5); ?>
</ul>

Update:

If you want to show the list of previous posts right after your article, then you need to put my code under this line in single.php: < ?php the_content(); ? >. See the screenshot below:

The code can be placed even lower, it's depends on your blog design and from where you want to display your previous posts.

You can change the number of posts in the list by changing "5" to another digit and you can change "Previous posts" to your text. Also, you can change the look of the output by adding some CSS styles.

Optionally, you can add some additional information about posts (date and number of comments).

Example of code:

<ul>
<h3>Previous posts:</h3>
<?php webdesignzo_previous_posts (5, '{a}{title}{/a} (Comments: {comments}) | {date:j.M.Y}'); ?>
</ul>

previous posts Wodrpress hack exampleYou can see the example of working "Previous posts" WordPress hack at the end of each post on my blog (under "About author" section).

Later I will write a post about how to do the same Hack, but with filter for categories. This hack will show previous posts from current category.

This is only one of the existing methods of internal SEO optimization which you can use in combination with other tricks and WordPress plugins. Post your comments if you want to share some more tricks or if you have any questions about installation of this hack.

About Alex Joy

I'm founder and editor of Webdesignzo, architect designer, web designer, graphic designer and SEO optimizer.

17 Responses to Internal Linking Optimization of WordPress Blog Posts Without Plugins

  1. Martin Smith says:

    I am trying to do exactly as you explain above but i cannot get it work, is there any chance you could assist or help in any way?

  2. Nikunj says:

    Previous post trick looks really neat & I think I will help in seo for sure, will try it on my blog.

    Thanks :)

  3. James says:

    I seem to be having the same problem as Martin. I tried placing the code at various locations throughout the file but the output’s always the same. Would you be willing to give a brief description of where the code should be placed in relation to the other code? Alternatively, are you aware of any plugin that accomplishes the same end? I’ve been searching around and can’t seem to find one. If you could do this I would be personally grateful and I’m sure everyone else experiencing the same issue (Martin and I can’t be the only ones) would greatly appreciate it as well. But anyway, thanks for the awesome tutorial. I’m sure once I figure it out it will be very helpful.

    • Alex Joy says:

      I’ve updated the description of the process (the code is also updated a little bit, to make it easy for you). I’ve solved the problem of Martin (he had encoded function.php file and he was inserting the code to the wrong place).

      There is no such plugin to do the same trick, I’ve wrote this code because of that :) I’d like to create one, but I never did this before.

      Leave a comment, If there will be any problems with placing the code.

  4. Jay Thornton says:

    any way to add thumbnails to this? I’m monkeying around with adding the_post_thumbnail but it’s not behaving. rather than adding one thumb to each of (6 in my example) it adds six thumbs above the first .

  5. zid says:

    Hi there, great tutorial… where should I put my CSS? i want to change the color of the links? And how can i remove those dots like yours? thanks a lot…

  6. Albi says:

    How to show 5 previous posts only from the category of the post?

  7. Ralph Lee says:

    I love your knowledge and power with CODE, but can I just manually link to all my post in the manner you have shown in your diagram until I figure out how to utilize your coding? Your intellect is so far out there and so easy for you, that I will have to think on things for a while. However, keep it all coming, okay. I’ll catch on!
    This is all pretty new to me, but I’m so excited to learn more every day…
    Thanks,
    Ralph

  8. adumpaul says:

    Great useful tutorials.Thank you.

  9. Rahul says:

    Before reading this post I was only using related posts plugin but now I learn how to make better internal linking structure by showing previous posts.
    Thanks Alex Joy for this great tutorial. :-)

  10. I think we all have benefitted from your code insertion so thanks, but there is obviously alot more to wordpress seo that just internal linking. Do you have any pages on this?

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>