If you're running a large blog, you'll need to find a way to help your readers find content that they're going to want to read, based on what they're currently reading or have just read.
One way to do this is by using one of the many plugins out there that identify related posts. These use various methods to identify other posts which have similar content to the current one, but sometimes they don't give you quite the results you want.
One logical way to identify posts like the one currently on display is by using categories, tags or taxonomy terms. If you could identify the posts in the same categories as the current one, you could then display a list of the most recent posts in the same categories, to help readers find related content.
In this tutorial I'll show you how to create a plugin to do just that. The plugin will give you a function which you then add to your single.php
template file to show the list under the content of the current post.
What You'll Need
To follow this tutorial you'll need:
- a development installation of WordPress
- an existing theme with a
single.php
template file - a code editor
Setting Up the Plugin
Let's start by setting up the plugin. Create a new file for your plugin—I'm calling mine tutsplus-related-posts.php
.
In your new file, add the following:
<?php /** * Plugin Name: Tuts+ Related Posts * Plugin URI: http://code.tutsplus.com/tutorials/creating-a-list-of-posts-with-the-same-categories-as-the-current-one--cms-22626 * Description: This plugin identifies what categories the current post is in and outputs a list of the most recent posts in the same categories. * Version: 1.0.0 * Author: Rachel McCollin * Author URI: http://rachelmccollin.com * License: GPL-3.0+ * License URI: http://www.gnu.org/licenses/gpl-3.0.html * Domain Path: /lang * Text Domain: tutsplus */
You'll want to change the author details to your own details, but this gives WordPress what it needs to recognise the plugin and enable you to activate it in the dashboard.
Identifying the Current Post's Categories
In order to identify other posts with the same categories, you need to get the list of categories the current post is in, and add them to an array.
Start by creating the function to hold your code, along with a few initializations:
function tutsplus_related_posts() { $post_id = get_the_ID(); $cat_ids = array(); }
Now inside your function, use the get_the_category()
function to fetch an array of data relating to the categories the current post is in:
$categories = get_the_category( $post_id );
The function has just one parameter, the post id, which specifies that it's the current post whose categories you want to get.
Next, you need to create an array which will hold the IDs of all of the categories:
if ( $categories && ! is_wp_error( $categories ) ) { foreach ( $categories as $category ) { array_push( $cat_ids, $category->term_id ); } }
Let's take a look at what this code does:
- First it checks that
$categories
hasn't returned null or an error. - Then for each category, it adds the ID to the array.
Your function will now look like this:
function tutsplus_related_posts() { $post_id = get_the_ID(); $cat_ids = array(); $categories = get_the_category( $post_id ); if ( $categories && !is_wp_error( $categories ) ) { foreach ( $categories as $category ) { array_push( $cat_ids, $category->term_id ); } } }
Writing a Query to Output Posts in the Same Category
Now that you have an array with the category IDs in it, you can use it as an argument for a query to identify posts with those categories.
Still inside your function, set the arguments for the query:
$current_post_type = get_post_type( $post_id ); $args = array( 'category__in' => $cat_ids, 'post_type' => $current_post_type, 'posts_per_page' => '5', 'post__not_in' => array( $post_id ) );
These arguments will find posts of the same post types as the current one, in the same categories, and exclude the current post. I've set it to output five posts, but you could always change this, or any of the other arguments such as post type if you've applied categories to more than one post type.
Now run the query:
$query = new WP_Query( $args ); if ( $query->have_posts() ) { ?> <aside class="related-posts"> <h3> <?php _e( 'Related Posts', 'tutsplus' ); ?> </h3> <ul class="related-posts"> <?php while ( $query->have_posts() ) { $query->the_post(); ?> <li> <a href="<?php the_permalink(); ?>"> <?php the_title(); ?> </a> </li> <?php } ?> </ul> </aside> <?php } wp_reset_postdata();
This outputs the post titles inside links to them, in a list.
Adding the Function to Your Theme
The final step is to add the function to a template file in your theme. I've created a child theme for the Twenty Fourteen theme with a new single.php
file, and I'm adding the function there, below the content. It can be added anywhere, as long as it's within the WordPress post loop.
In your template file, add the following:
tutsplus_related_posts();
You can see the result below:
Note: The demo site is a little confusing as tags and categories are listed at the end of each post. Our list doesn't use tags, just categories, of which each post in this site has one.
Alternatives: Tags and Taxonomy Terms
You can quite easily alter this function to work with tags or taxonomy terms instead.
To identify tags instead of categories, you would replace the get_the_category()
function with get_the_tags()
, and then replace the 'category__in'
argument for the query with 'tag__in'
.
To identify taxonomy terms, you'd use get_the_terms()
in place of get_the_category()
, adding the taxonomy slug as the second parameter. You'd then replace the 'category__in'
argument with an argument using 'tax_query'
.
You could also search for posts with the same terms in more than one taxonomy (including terms and categories), by creating two arrays and using two arguments in your query.
Summary
In this tutorial you've learned how to identify the categories the current post is in, add their IDs to an array, and then use that array to run a query displaying posts in the same category. This gives you more control than you get from most related posts plugins, and means you can define the arguments for your query so that the posts are displayed in exactly the way you want.
You can also adapt this technique to work with tags, taxonomy terms, or a combination of two or more.
Comments