Hi, I'm a web developer and blogger from Russia. My nickname is Dimox.
Sorry for my English, it's not my native. Read more about me and my blog.

A Great Way of WordPress Internal Linking for SEO

A Great Way of WordPress Internal Linking for SEO

As you may already know, web site’s internal linking is one of the important methods for boost his search engine ranking. So you should not neglect this, if you have such opportunity.

One my familiar blogger gave me a very great idea about internal articles linking, which he implemented on his web sites.

I decided to make this thing for WordPress, because I am using this blogging engine on my blogs.

What is the essense of idea? We put on the blog an additional block with the list of previous articles from the same category chronologically (PHP-script looks publication date). For example, if we show the 5 previous articles and the current article have an ID = 7, so in this list we will see articles with IDs 6, 5, 4, 3 and 2.

But it’s not all. I did also the ring linking structure, i.e. if current article is the first from the category, in the list we’ll see the most recent posts from that category. For example, category have 10 articles and we show again 5 posts in “previous”. So when we get on the 3rd article, “previous from the category” will contain the links on articles: 2, 1, 10, 9 and 8.

As a result each article will recieve the same number of links to itself (5 permanent links, if look on example above) and all articles fall in to web site internal linking.

Now lets take a look on the code (do not be alarmed, it’s quite big):

<?php
$li = 5; // number of links to show in the list
$postID = $post->ID;
$postDate1 = get_the_time('YmdHis','','',false);
$cat = get_the_category(); $cat = $cat[0]; $cat = $cat->cat_ID;
$catQuery = new WP_Query('showposts=-1&cat='.$cat);
$i = 0;
if ($catQuery->have_posts() && $catQuery->post_count > 1) :
?>
<ul>
<?php
  while ($catQuery->have_posts()) : $catQuery->the_post();
    $postDate2 = get_the_time('YmdHis','','',false);
    if ($postDate2 < $postDate1 && $post->ID != $postID) {
      $i++;
      if ($i <= $li) {
?>
  <li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>
<?php
      }
    }
  endwhile;
?>
<?php
  $posts = $li - $i;
  $cat_count = get_category($cat)->category_count;
?>
<?php
  if (($cat_count - 1) > $i) {
    if ($posts > 0) {
      $temp_query = $wp_query;
      if (($cat_count - 1) < $li) $posts = $cat_count - 1 - $i;
      query_posts('showposts='.$posts.'&cat='.$cat);
      if (have_posts()) : while (have_posts()) : the_post();
?>
  <li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>
<?php
      endwhile; endif;
      $wp_query = $temp_query;
    }
  }
?>
</ul>
<?php endif; wp_reset_query(); ?>

This code you need to put in the single.php file of your WordPress theme. I hope you are familiar with the structure of WP themes? =) If no, WordPress Codex will help you.

The template of the link in this code is looks like (it comes across 2 times):

<li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>

In this template you can use the same PHP tags like in default WordPress loop.

I have tested the code on WordPress 2.8.x and can’t guarantee the work on another verisons.

Cоmmеnts (4):
  1. 1

    If you find an alternative for the russian script “orphus” (http://orphus.ru/), or just translate it into Russian, I can corret every mistake I find. (There are lots of them, please, don’t be offended).

  2. 2

    Hi dimox.., this code is very interesting.., i have installed it to my blog, I manage to call this code at my single post.. , But in some case, i got fatal error when my single post is applied to more than one category..: For example :
    My post title : Internal link for SEO >> include in two category ( WordPress, SEO )

    if i call your code for this post i got fatal error…, can you help me for this..?
    Thanks

  3. 4

    hi dimox.., i think my problem up there was solved..

    Anyway i have an idea, instead of showing related from same category, i want to show related from same tags ( show only the tags that is related with single post ). i dunno is it to pull from same category or same tags. can you show us how to code this..? Thanks

Lеаvе а Соmmеnt

© 2009–2016 Dimox.net  •  Privacy Policy