Truemag

  • Categories
    • Tips And Tricks
    • Internet
    • PHP
    • Javascript
    • CSharp
    • SQL Server
    • Linux
  • Lastest Videos
  • Our Demos
  • About
  • Contact
  • Home
  • Write With Us
  • Job Request
Home Tips And Tricks Generate And Display Related Content On WordPress

Generate And Display Related Content On WordPress

Automatically generate and display related content whenever you create a new post can keep visitors stay longer on your site. They will get more useful information with list of content similar to the current page they’re viewing. Plus, you also have chance to show how best your website/blog is. It’s really a very good idea, isn’t it?

In this tutorial, I’ll show you 2 ways to do that:

1. Do it yourself

On this method, you need to make some changes on your single.php file in your WordPress Theme Folder. The main idea is will get list of posts have same tags with the current post. Of course, you may custom the algorithm to get the most relevance that suit with your expectation such as they are must on the same category, etc…

Place example code below in the bottom of your post content in your single.php file:

<?php
	$posttags = get_the_tags();
 
	if ($posttags) {
 
		$tags_arr = array();
		foreach($posttags as $tag) {
			$tags_arr[] = $tag->slug; 
		}
 
		$args = array(
			'orderby' => 'date',
			'order' => 'desc',
			'posts_per_page'  => 10,
			'caller_get_posts' => 1,
			'tag_slug__in' => $tags_arr
		);
 
		query_posts($args);
		if (have_posts()) :
?>
<h3>Related content you should read</h3>
<div>
	<ul>
		<?php while (have_posts()) : the_post(); ?>
		<li><a title="<?php the_title();?>" href="<?php the_permalink() ?>"><?php if ( get_the_title() ) the_title(); else the_ID(); ?> </a></li>
		<?php endwhile ?>
	</ul>
</div>
<?php
		endif;
		wp_reset_query();
	}
?>

<?php $posttags = get_the_tags(); if ($posttags) { $tags_arr = array(); foreach($posttags as $tag) { $tags_arr[] = $tag->slug; } $args = array( 'orderby' => 'date', 'order' => 'desc', 'posts_per_page' => 10, 'caller_get_posts' => 1, 'tag_slug__in' => $tags_arr ); query_posts($args); if (have_posts()) : ?> <h3>Related content you should read</h3> <div> <ul> <?php while (have_posts()) : the_post(); ?> <li><a title="<?php the_title();?>" href="<?php the_permalink() ?>"><?php if ( get_the_title() ) the_title(); else the_ID(); ?> </a></li> <?php endwhile ?> </ul> </div> <?php endif; wp_reset_query(); } ?>

The code above will show top 10 posts have the same tag with the current page. Let’s see a preview of a article on my site: http://4rapiddev.com/problem-issue-error/dymanic-crm-error-401-unauthorized-when-create-or-retrieve-account-and-contact/

Wordpress Related Content Preview

Wordpress Related Content Preview

If you just want to display related posts within the same category with the current post, follow code below:

<?php
	$posttags = get_the_tags();
	$categories = get_the_category();
 
	if ($posttags) {
 
		$tags_arr = array();
		foreach($posttags as $tag) {
			$tags_arr[] = $tag->slug; 
		}
 
		if ($post_categories)
			foreach($categories as $category)
				$category_ids = $category->term_id . ",";
 
		$args = array(
			'orderby' => 'date',
			'order' => 'desc',
			'posts_per_page'  => 10,
			'caller_get_posts' => 1,
			'tag_slug__in' => $tags_arr,
			'cat' => $category_ids
		);
 
		query_posts($args);
		if (have_posts()) :
?>
<h3>Related content you should read</h3>
<div>
	<ul>
		<?php while (have_posts()) : the_post(); ?>
		<li><a title="<?php the_title();?>" href="<?php the_permalink() ?>"><?php if ( get_the_title() ) the_title(); else the_ID(); ?> </a></li>
		<?php endwhile ?>
	</ul>
</div>
<?php
		endif;
		wp_reset_query();
	}
?>

<?php $posttags = get_the_tags(); $categories = get_the_category(); if ($posttags) { $tags_arr = array(); foreach($posttags as $tag) { $tags_arr[] = $tag->slug; } if ($post_categories) foreach($categories as $category) $category_ids = $category->term_id . ","; $args = array( 'orderby' => 'date', 'order' => 'desc', 'posts_per_page' => 10, 'caller_get_posts' => 1, 'tag_slug__in' => $tags_arr, 'cat' => $category_ids ); query_posts($args); if (have_posts()) : ?> <h3>Related content you should read</h3> <div> <ul> <?php while (have_posts()) : the_post(); ?> <li><a title="<?php the_title();?>" href="<?php the_permalink() ?>"><?php if ( get_the_title() ) the_title(); else the_ID(); ?> </a></li> <?php endwhile ?> </ul> </div> <?php endif; wp_reset_query(); } ?>

2. Use a WordPress Plugin

There are some plugins on both WordPress.org and the Internet but I just suggest 2 plugins I like best.

  • 1. Yet Another Related Posts Plugin
  • 2. WordPress Related Posts

But my favorite one is the Yet Another Related Posts Plugin and it’s being used on the site because some of features below:

  • Easy to install
  • Don’t need to make any changes
  • The related posts are related
  • The algorithm is good
  • Full customizable with lot of options

I captured some screens for you can reference:

Yet Another Related Posts Plugin Preview

Yet Another Related Posts Plugin Preview

Yet Another Related Posts Plugin Preview

Yet Another Related Posts Plugin The Pool

Yet Another Related Posts Plugin The Pool

Yet Another Related Posts Plugin The Pool

Yet Another Related Posts Plugin Relatedness Options

Yet Another Related Posts Plugin Relatedness Options

Yet Another Related Posts Plugin Relatedness Options

Yet Another Related Posts Plugin Display Options

Yet Another Related Posts Plugin Display Options

Yet Another Related Posts Plugin Display Options

Apr 2, 2011Hoan Huynh
Dymanic Crm Error 401 Unauthorized When Create Or Retrieve Account And ContactPlace Twitter Tweet Button On Wordpress
You Might Also Like:
  • How To Get WordPress Tags By Categories
  • WordPress Filter/Exclude Posts Category From Displaying In A Specific Page
  • WordPress Check If Post Is In Category
  • How To Group Related Options In A Drop Down List
  • Manage Or Organize WordPress Media Library Structure In Categories
  • Sending Email With SMTP Authentication In WordPress
  • Excerpt box disappeared on WordPress New Version
  • Top Plugins Should Be Installed On WordPress
  • WordPress View Large Image Without Leaving Current Page
  • WordPress Get Recent Posts And Recent Comments
Hoan Huynh

Hoan Huynh is the founder and head of 4rapiddev.com. Reach him at [email protected]

11 years ago Tips And Tricksget_the_category, get_the_tags, have_posts, query_posts, the_permalink, Wordpress, WordPress Related Posts, Yet Another Related Posts Plugin278
0
GooglePlus
0
Facebook
0
Twitter
0
Digg
0
Delicious
0
Stumbleupon
0
Linkedin
0
Pinterest
Most Viewed
PHP Download Image Or File From URL
24,451 views
Notepad Plus Plus Compare Plugin
How To Install Compare Text Plugin In Notepad Plus Plus
21,829 views
Microsoft SQL Server 2008 Attach Remove Log
Delete, Shrink, Eliminate Transaction Log .LDF File
17,646 views
JQuery Allow only numeric characters or only alphabet characters in textbox
14,984 views
C# Read Json From URL And Parse/Deserialize Json
11,710 views
4 Rapid Development is a central page that is targeted at newbie and professional programmers, database administrators, system admin, web masters and bloggers.
Recent Posts
  • Things to Learn about Installingderm Loan Type S
  • Online Photo Editor – Free Photoediting Software
  • A Guide to Finding the Best Paper Sellers
  • Photoediting in Home Isn’t Hard to Do!

  • Free Photo Editor Online
Categories
  • CSharp (45)
  • Facebook Graph API (19)
  • Google API (7)
  • Internet (87)
  • iPhone XCode (8)
  • Javascript (35)
  • Linux (27)
  • MySQL (16)
  • PHP (84)
  • Problem Issue Error (29)
  • Resources (32)
  • SQL Server (25)
  • Timeline (5)
  • Tips And Tricks (141)
  • Uncategorized (647)
Recommended
  • Custom Software Development Company
  • Online Useful Tools
  • Premium Themes
  • VPS
2014 © 4 Rapid Development