Pin It

How to: Show Similar articles in WordPress (without plugin)

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.

wordpress

 

 

 

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)
{
global $post;

$tags = get_the_tags();

if($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)
{
setup_postdata($tmp_post);
$title = get_the_title($tmp_post->ID);
$output .= ‘<li><a href=”‘.get_permalink($tmp_post->ID).'” title=”Artikel &bdquo;’.$title.’&ldquo; lesen”>’.$title.'</a></li>’;
}

wp_reset_postdata();
}

$output = !empty($output) ? $output : ‘<li>Keine √§hnlichen Artikel gefunden.</li>’;
return ‘<ul>’.$output.'</ul>’;
}

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.

Conclusion

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.

Filed in: Blog, Wordpress Tags: 

You might like:

How to: Show Recent Articles in WordPress (without plugin / widget) How to: Show Recent Articles in WordPress (without plugin / widget)
How to: Show Latest Comments in WordPress (without plugin/widget) How to: Show Latest Comments in WordPress (without plugin/widget)
How to: Show Top Commentators in WordPress (without plugin) How to: Show Top Commentators in WordPress (without plugin)
How to protect a WordPress blog from hacker attacks, increase security! How to protect a WordPress blog from hacker attacks, increase security!

Leave a Reply

Submit Comment

© 2017 TechieJerry. All rights reserved. XHTML / CSS Valid.
Proudly designed by Theme Junkie.