Archived topic
Next/Previous links within category only?
15 replies · Started by Helen on December 1, 2015
Hi Tom
I have a complex site organised by categories. I'd like the next/previous links at the bottom of each post to only lead to next/previous posts within the category of the current post, rather than just linking to the next-dated post regardless of category.
Ive searched and searched but there seems to be no plugin to achieve this. There are various folks online who offer some code to achieve it but I'm afraid I can't follow them.
Would you be able to give one of your nice explanations of exactly what code needs to be written where to achieve this? I have the code snippets plugin and have been able to follow your instructions before just perfectly.
No hurry but if you can help I would be massively grateful!
Hi Helen,
Not easy currently, but I've made it easy in the next update :)
In GP 1.3.22, you'll be able to do this:
add_filter( 'generate_category_post_navigation','generate_force_category_post_navigation' );
function generate_force_category_post_navigation()
{
return true;
}
If you need it right now, you can replace the generate_content_nav() function in the template-tags.php folder with this new function: http://pastebin.com/4AUwQt78
Let me know if you need more info :)
That is great news. By my calculations 1.3.22 is the next update so I am happy to wait for that.
Than all I will need to know is where to write your code snippet!
Thanks a million.
Helen
It may be a week or two before the next update.
For the code snippet, this should help: https://generatepress.com/knowledgebase/adding-php-functions/
So what I did was I followed your pastebin link above, copied the code from RAW PASTE DATA, opened ADD NEW SNIPPET in code snippets, pasted the code in there and clicked save+activate.
The next/prev links still showed the next/prev dated posts even if these are not in the same category.
So then I went to the EDIT THEMES page, found the Generate Press: templates-tags.php file, located the section that corresponded to if(!function_exists('generate_content etc from the pastebin code, and replaced it with the pastebin code.
I felt very proud of myself figuring all this out and was sure it was going to work - but no, the links still go to the next/prev date even outside the category.
I dont mind waiting a week or so for the next update - but if there is something I have done wrong that is easy to spot and fix, please let me know!
Thanks for your patience
Helen
Hi Helen,
What you did by finding the template-tags.php file and replacing it sounds great!
Did you also add this code snippet to the Code Snippets plugin?: https://generatepress.com/forums/topic/nextprevious-links-within-category-only/#post-157100
Aha - so that has to go in too. I thought that was just for when the updated version 1.3.22 came out.
All works perfectly now, and should save a bit of confusion for users. Thanks again!
Helen
Awesome, glad it's working! :)
Now that GP is on 2.1.4 version do we have an easier way to enable Next/Previous links within category only (without the filter you suggested above)?
The filter is still the only way.
Hi Tom,
I'm using the filter above on GP 2.2.2
add_filter( 'generate_category_post_navigation','generate_force_category_post_navigation' );
function generate_force_category_post_navigation()
{
return true;
}
My question is, how to display the navigation if that category only contain 1 post?
Thank you!
Hi there,
I'm not sure if that's possible, as the navigation wouldn't have any posts to link to. Any examples?
Let say that currently that particular category only have 1 post, can we add if statement for the filter to look for all posts? I am trying to figure that out :)
So if other posts exist, keep it within the category. If not, show the link regardless of the category?
I just checked out the function and I'm not sure it's possible, unfortunately.
Yes exactly. Alright I will use the default one then.
Thanks for your support Tom. Love your work!
