- This topic has 15 replies, 4 voices, and was last updated 7 years, 1 month ago by
Tom.
-
AuthorPosts
-
December 1, 2015 at 12:56 am #156968
Helen
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!December 1, 2015 at 10:36 am #157100Tom
Lead DeveloperLead DeveloperHi 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 🙂
December 1, 2015 at 2:15 pm #157143Helen
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.
HelenDecember 1, 2015 at 10:21 pm #157190Tom
Lead DeveloperLead DeveloperIt may be a week or two before the next update.
For the code snippet, this should help: https://generatepress.com/knowledgebase/adding-php-functions/
December 1, 2015 at 11:50 pm #157200Helen
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
HelenDecember 2, 2015 at 12:12 am #157204Tom
Lead DeveloperLead DeveloperHi 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
December 2, 2015 at 3:51 am #157254Helen
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!
HelenDecember 2, 2015 at 10:20 am #157330Tom
Lead DeveloperLead DeveloperAwesome, glad it’s working! 🙂
September 14, 2018 at 5:33 pm #677560Rui
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)?
September 14, 2018 at 8:21 pm #677593Tom
Lead DeveloperLead DeveloperThe filter is still the only way.
February 12, 2019 at 2:00 pm #808710Arif Hazwan
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!
February 12, 2019 at 5:17 pm #808828Tom
Lead DeveloperLead DeveloperHi there,
I’m not sure if that’s possible, as the navigation wouldn’t have any posts to link to. Any examples?
February 12, 2019 at 6:11 pm #808853Arif Hazwan
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 🙂
February 13, 2019 at 8:33 am #809481Tom
Lead DeveloperLead DeveloperSo 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.
February 13, 2019 at 8:40 am #809495Arif Hazwan
Yes exactly. Alright I will use the default one then.
Thanks for your support Tom. Love your work! -
AuthorPosts
- You must be logged in to reply to this topic.