On my WordPress post view page, I wish to show posts in the same category as the currently selected post.
My difficulty here was managing this to handle multiple categories, but also, a way to track which was the ‘category’ page that you viewed the page at.
For example, a standard route though the website would be Homepage > Category Page > Post, but for posts that sit in multiple categories, how do I know which category the user viewed before hitting the Post page, so that I can show related posts in that particular category.
I came up with a solution that made use of the wp_get_referer() method.
This returns the previous page URL.
With this, I have to handle if the page was visited from Google, and if the page was visited from another route in the website and make appropriate adjustments.
Functions
I created a function called referred_category to handle this.
Then I could simply call it from my post template file.
I passed into this function, the wp_get_referer() return value and the post ID for the current page.
|
1 |
$chosenCategory = referred_category(wp_get_referer(), get_the_ID()); |
The basic structure of this function is as follows:
|
1 2 3 4 5 6 7 8 9 |
function referred_category($url, $postID) { // Compare Referred URL with Site URL to check that the previous page was on the same website // if true, get the 2nd to last slug from the url (usually the category) // and check if it is indeed a category page //if it is, return the category ID //if it isn't, use current permalink to decide on a category, not referred URL // if previous url wasn't of the same website (search engine or other referral) // use current permalink to decide on a category, not referred URL. } |
Here is my actual function, with an included get_post_primary_category() function for deciding on the category id of the current post.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
function referred_category($url, $postID) { $arrayOfComponents = split("/", $url); $arrayOfSiteComponents = split("/", site_url()); if ($arrayOfComponents[2] == $arrayOfSiteComponents[2]) { $slug = $arrayOfComponents[sizeof($arrayOfComponents) - 2]; $cat = get_category_by_slug($slug); if ($cat) { return $cat->term_id; } else { return get_post_primary_category($postID); } } else { return get_post_primary_category($postID); } } function get_post_primary_category($id) { $permalink = get_permalink($id); $arrayOfComponents = split("/", $permalink); $slug = $arrayOfComponents[sizeof($arrayOfComponents) - 3]; $cat = get_category_by_slug($slug); return($cat->term_id); } |
Now all I have to do is use that category ID in a loop on my post page and I can return the category posts as I wanted.
|
1 2 3 4 5 6 7 8 9 10 |
<div id="article-list"> <?php query_posts($query_string . '&cat=' . $chosenCategory); ?> <?php while (have_posts()) : the_post(); ?> <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>> <header class="entry-header"> <h1 class="entry-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1> </header> </article> <?php endwhile; ?> </div> |
Flaw in my plan
Okay, in my get_post_primary_category() function, I request the category from the permalink to the current post.
What if that post is part of multiple categories? How do I decide which of the categories I’d like to show as ‘default’?
WordPress automatically decides upon the category to show in the permalink based upon the id numbers of the categories.
I found a plugin called Hikari Category Permalink which allows us to decide on which of my multiple post categories I’d like to use as the permalink.
It took some hours of research and some helpful plugins but I got the functionality I need complete!
Comments
Powered by Facebook Comments