Interlinking Posts in WordPress (Previous Posts from Category)

In this article, I want to share another WordPress function whose task includes displaying previous entries from the category of the current article. The function creates circular interlinking.

One of the feature of this function is that it displays previous entries from the category.

An alternative to this function can be found at this link, where you can specify taxonomy and post type for which interlinking should occur.

I have published a similar function in the comments on the website Dimox.name here. Why "similar"? Because this function has several advantages over the one published on Dimox.name, namely:

  1. It is possible to define the output format, making it very easy to integrate into any template;
  2. There is no need to pre-determine the current category for this function (it will determine it itself), meaning less unnecessary code in the template and simpler for beginners.
  3. The function does not use the heavy WordPress function get_posts().
  4. Classes li1 and li2 are added to each link tag to easily style the list in a zebra pattern.
  5. Caching can be enabled. More details below.
  6. The list is sorted by date, not by ID, so if a post was published at a later date, it will be displayed as required.

Using the Function

Here is the code that needs to be placed in your template file functions.php.

After successfully copying the code into the theme file functions.php, where you want to display the previous entries from the current category, call the function like this:

<ul>
	<?php kama_previous_posts_from_cat('post_num=5');  ?>
</ul>

That's all you need to do for simple use of the function.

Important! This call will work correctly only in the theme file responsible for displaying posts, usually single.php.

Advanced Usage

For setting the output format, use

You can use the following shortcodes in the format parameter:

  • {thumb} - the post's thumbnail (it must be set for the post);
  • {comments} - will show the number of comments for the article;
  • {title} - the article's title;
  • {date:j.M.Y} - date in the format j.M.Y (11.Apr.2010);
  • {a} and {/a} - link tags, opening and closing.

The call would be like this:

<ul>
<?php
kama_previous_posts_from_cat( array(
	'post_num' => 5,
	'format' => '{a}{title}{/a} - {date:j.M.Y} // {comments}',
) );
?>
</ul>

Will display the list in the format:

<li class='li1'>
	<a href='http://link' title='Article title'>Article title</a> - date // number of comments
</li>
Using Cache

Caching is enabled by default, but you can benefit from using it if the function is called multiple times or if an object caching plugin is enabled: WP File Cache, SJ Object Cache and others...

List Tag

You can change the list tag from li to any other, for example, <div>

<?php kama_previous_posts_from_cat('post_num=5&list_tag=div'); ?>

// will output 5 links in the format - <div class='li1'><a href='http://link' title='Article title'>Article title</a></div>.

Changes

Version 1.0.

  • Added the {thumb} shortcode in the format parameter
  • Now all arguments are passed in the first function parameter, as an array or string.