A good way in your WordPress blog to optimize the internal linking and increasing the time to visit your visitors to view other matching items to an article on the subject is.
Similar products are determined in general by category and/or tags. In WordPress, there are some good plugins with this functionality, which are already available. Who would not necessarily like to install another plugin, you should read this article in which I will show you how to display similar articles based on the tags.
Tips: How to: Show Latest Comments in WordPress (without plugin/widget)
The code for determining the similar products
In our unctions.php , we add the following code:
function get_related_posts($limit = 5)
$tags = get_the_tags();
$tag_ids = array();
foreach($tags as $tag)
$tag_ids = $tag->term_id;
$args = array(
‘numberposts’ => $limit,
‘tag__in’ => $tag_ids,
‘exclude’ => $post->ID
$related_posts = get_posts($args);
$output = ”;
foreach($related_posts as $tmp_post)
$title = get_the_title($tmp_post->ID);
$output .= ‘<li><a href=”‘.get_permalink($tmp_post->ID).'” title=”Artikel „’.$title.’“ lesen”>’.$title.'</a></li>’;
$output = !empty($output) ? $output : ‘<li>Keine ähnlichen Artikel gefunden.</li>’;
The first part of the function (line 3-21) is used to pick up based on the tags with topics relevant item from the database. For this we first identify the tags of the article (line 5), which the visitor has called up and store the associated tag IDs in an array (lines 9-13).
Then we fire using the function get_posts () , which we pass an array with various options, a database query (lines 15 – 21). For the request, we specify that only a certain number ( $ limit ) of articles are fetched from the database. By default theme 5 related products to be determined.
Furthermore, we give our tag IDs and define that the current item, the visitor has called, is excluded.
In the second part (lines 23 – 36) we simply add our issue together. Here we create a list and add a link for all of the detected items.
The code to display the featured products
To insert the list of items under your articles, the following code within the WordPress loop has to be inserted:
1. echo get_related_posts (6);
In this case would, if present, six themes related products are displayed.
The presented code is of course again just an example and can be customized according to your wishes.
For example, it is also possible to display the still image with each item or similar items are not based on the tags, but based on the category (ies) to be determined.
A full-time blogger & Digital Marketing Expert in India. An early adopter who loves to try out various Digital, Blogging & Social media tools along with exploring some cool iOS and Android apps.