Cyber Week Sale! Get $20 off GP Premium, $40 off our new Lifetime license, and 45% off license renewals/extensions! Learn more

[Resolved] Next/Previous links within category only?

Home Forums Support Next/Previous links within category only?

Viewing 15 posts - 1 through 15 (of 16 total)
  • Author
    Posts
  • #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!

    #157100
    Tom
    Lead Developer
    Lead Developer

    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 🙂

    #157143
    Helen

    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

    #157190
    Tom
    Lead Developer
    Lead Developer

    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/

    #157200
    Helen

    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

    #157204
    Tom
    Lead Developer
    Lead Developer

    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

    #157254
    Helen

    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

    #157330
    Tom
    Lead Developer
    Lead Developer

    Awesome, glad it’s working! 🙂

    #677560
    Rui

    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)?

    #677593
    Tom
    Lead Developer
    Lead Developer

    The filter is still the only way.

    #808710
    Arif 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!

    #808828
    Tom
    Lead Developer
    Lead Developer

    Hi there,

    I’m not sure if that’s possible, as the navigation wouldn’t have any posts to link to. Any examples?

    #808853
    Arif 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 🙂

    #809481
    Tom
    Lead Developer
    Lead Developer

    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.

    #809495
    Arif Hazwan

    Yes exactly. Alright I will use the default one then.
    Thanks for your support Tom. Love your work!

Viewing 15 posts - 1 through 15 (of 16 total)
  • You must be logged in to reply to this topic.