Custom Method to Allow Your Authors to Choose Related Posts in WordPress
The amplitude of features and customization abilities that WordPress provides isn’t hidden anymore. Every individual and developers alike are well aware how great WordPress is – for website creation and maintenance. Not only that, website owners who want a simple blog can also use WordPress.
Another flexible feature that WordPress offers is related posts. Displaying related posts on your site helps save your users’ time – to navigate throughout your website – searching for topics that relates to a post they might be interested in.
By default, WordPress allows you to display related posts in two ways: via tags and categories. However, there is a downside associated with these ways. Both tags as well as categories make use of automated process to show the related posts in your website. But, what if you wish to show your own choice of related posts? What if you want to display related posts of a single author or multiple authors? In that case, WordPress customization is a way which comes to the scene. By implementing a custom method to display related posts will prove a viable solution.
This post is about how you can create a custom method, allowing your website authors to choose related posts in WordPress.
How to Create Custom method Allowing Authors to Choose Related Posts?
For creating a custom method that allows authors to choose related posts, you’ll first have to create a metabox in WordPress post screen, display all the posts in that screen. And then, allow your website authors to choose related posts from the list of posts. For doing so, paste the below code in your functions.php file:
Beginners might find the above code a bit hard to understand. So, let’s break the code down into different steps, so that you can get a better idea of how it works.
Step 1 – Registering Our Custom Metabox
The very first line in our code creates a wp_related_post_meta_box() function that helps register our custom metabox. And the add_meta_box() function take some parameters to tell WordPress about the metabox. Each parameter in the add_meta_box() function has a certain meaning to it. Let’s now take a look at each of those parameters:
wp_related_post: It determines the HTML ‘id’ attribute of the WordPress post editor screen.
Select Related Post: It specifies the title of the editor screen.
wp_related_post_list: This function prints out the HTML for the editor screen.
post: This parameter determines that the metabox will appear on post screen. However, you can even choose to display the metabox on page screen by using “page” instead of “post” as your parameter.
Lastly, the add_action( ‘add_meta_boxes’, ‘wp_related_post_list’ ) is an action hook that tells WordPress to add metabox.
Step 2 – Creating Function to Generate Metabox Output
The wp_related_post_list( $post ) function helps to generate output of our custom metabox. This code will generate a select list to allow your WordPress website authors to select related posts from the posts list, which will look something like:
Step 3 – Saving MetaBox
The (wp_save_meta_box_related_post( $post_id ) function is used to save the metabox value to the WordPress database. This function will take post id to save that metabox field value on the particular post meta session in your website database. And the add_action( ‘save_post’, ‘wp_save_meta_box_related_post’ ) is a hook that tells WordPress to save the post with meta value.
How to Display Related Post List On Frontend?
In order to display your choice of related posts list on your website frontend, simply paste the following code in your single.php file:
The above code will fetch the post meta value from your database. And the get_post_meta() function will get the value you have to pass for: Related Posts and meta key in this function.
Now, let’s see the below screenshot which displays the output of the above code:
So, that’s it! I hope that this post would help you understand the approach you can follow, to display your authors related posts on your WordPress website.
This entry was posted on Monday, April 6th, 2015 at 08:31 and is filed under Tutorials, Wordpress. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.
Leave a Response
We do love friendly, well-constructed and respective comments. We do not love bitchy, stupid and disrespectful comments. Find something wrong in this post?, feel free to send us a note and we will fix the issue asap.
Recent Comments