[Resolved] post-nav stay in current category & loop

Home Forums Support [Resolved] post-nav stay in current category & loop

Home Forums Support post-nav stay in current category & loop

Viewing 15 posts - 1 through 15 (of 16 total)
  • Author
  • #485032

    Hi, I have a gallery site organized by categories.
    I’d like the post-nav links on the single page posts to only lead to next/previous posts within the same category.
    I also would like everything to loop.. basically the last post’s ‘next’ to link back to the first post in the same category and the first post’s ‘previous’ to link to the last)..

    I’ve seen various old forum posts but after trying them it’s not working for me.
    Maybe there’s a GP 2.0 method that works best?

    LOVE GP… THX for any help!!!

    PS – FWIW I have a feeling the following might be the approach
    formated in:

    add_filter( 'filter_name', 'example_function_name' );
    function example_function_name()
        return 'Your new value';

    I tried but didn’t work.
    So if this the right code the question is how to code/format it correctly using Filters and where to place it in Hooks(?).

    Another old post from early 2016 had this suggestion.. and I tried it too using Snippets to place it but it didn’t work either:

    add_filter( 'generate_category_post_navigation','generate_force_category_post_navigation' );
    function generate_force_category_post_navigation()
        return true;
    GeneratePress 2.0.2
    Lead Developer
    Lead Developer

    Hi there,

    That should work.

    Try this instead:

    add_action( 'after_setup_theme', 'tu_category_specific_post_navigation' );
    function tu_category_specific_post_navigation() {
        add_filter( 'generate_category_post_navigation', '__return_true' );

    Let me know 🙂


    THX for prompt reply – as usual!!
    Sorry I tried it but next/prev now is going to Home page.
    I simply put it in Snippets.. should it be in Hooks, and does it need some <php> formating around it?

    Hmm, ya want my wp login?
    thanks Tom!!!

    Lead Developer
    Lead Developer

    The links don’t actually seem to be pointing to posts – they’re pointing to the homepage with an Elementor library query variable attached.

    Perhaps try deactivating your other plugins for now to see if there’s a conflict?


    OK.. fixed!
    Basically I fixed post-nav links to stay in category by not using Elementor’s ‘Post Navigation’ widget and making visible again GP’s post-nav.

    Details – It turns out I was using Elementor’s ‘Post Navigation’ widget (since it was so easy to style & place it in the page) which evidently is not listening to the theme’s post-nav code. Also in my css I had hid the display of your standard post-nav “#nav-below” since I was using Elementor’s .
    Ohh well, I guess not a good approach. 🙂

    Now my last request here is for the post-nav to loop.. where the last post’s ‘next’ links back to the first post in the same category
    and the first post’s ‘previous’ links to the last post.
    Can this be done somehow?

    BTW, any chance GP Customizer might someday have options for Single’s ‘post-nav’?… It’s such an important WP section that never seems to get much attention from all themes or plugins and so I’m always spending a lot of time tweaking it.. Options I’d love to see are: ‘stay in category’, ‘Loop in category’, align/float to sides/left/right.. Styling would be nice too, such as: ‘arrows’ style & color, re-title ‘previous/next’ and ‘use post title’.

    THANKS TOM!!!!


    BTW, besides wanting to loop the fixed post-nav
    can I also know how to add ‘previous’ & ‘Next’ beside the arrows please?
    (Right now it shows the Post name but I can just css those out.)

    Lead Developer
    Lead Developer

    Tough one! I believe you would have to overwrite the entire function.

    I took a look around and found a couple solutions, but they’re somewhat hacky. I’m surprised WP doesn’t have a built in mechanism for this.

    So, copy this highlighted code: https://github.com/tomusborne/generatepress/blob/2.0.2/inc/structure/post-meta.php#L12-L82

    And paste it into your functions.php, or another one of these methods: https://docs.generatepress.com/article/adding-php/

    Then, replace these two lines: https://github.com/tomusborne/generatepress/blob/2.0.2/inc/structure/post-meta.php#L50-L51

    With something like this: https://stackoverflow.com/a/34403977/2391422


    WOW! Thanks for all the effort Tom! You are amazing!!!

    I gave it a go, the name change worked but it didn’t loop.
    I started thinking about how I did this in the past… decided to try my old way using a plugin and gave it a go (using your highlighted un-altered code you mentioned above that I then put into a Snippet – thx!)… and low and behold everything’s working! – it’s looping & I can change various params like title, stay in category, etc.. I’m off to bed now but will report back here when I play with things for a while and see if all stays stable n all. But big thanks getting me the code and all!!

    This is the plug I used, a bit old but works great so far!:

    Lead Developer
    Lead Developer

    Awesome! Thanks for sharing those plugins! 🙂


    Glad I could contribute!
    As you can see I have some knowledge but big gaps too. ;P
    Hmm, maybe this plug will give you insights to an approach to adding this capability into GP customizer?

    For those that are wondering the steps I used… I simply:
    + activated the plugin,
    + pasted your ‘highligted code’ into a Snippet
    + & replaced the 2 lines you mentioned with

    c2c_next_or_loop_post_link( $format='&laquo; %link', $link='previous', $in_same_term = true, $excluded_terms = '', $taxonomy = 'category' );
    c2c_previous_or_loop_post_link( $format='%link &raquo;', $link='next', $in_same_term = true, $excluded_terms = '', $taxonomy = 'category' );

    + BTW, IF anyone would rather have the post title rather than ‘previous / next’ than use this code (which is what I modified to get things the way I wanted, there’s many more options at the plugin page):

    c2c_next_or_loop_post_link( $format='%link &raquo;', $link='%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' )
    c2c_previous_or_loop_post_link( $format='&laquo; %link', $link='%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' )

    So I have 1 last question please…
    I know this might be outside your support offerings but it might be useful to others who want to use this plug…
    How can I format the code I put in so I can access the 2 ‘previous/next’ parts via css? Right now it’s all in 1 css identifier.


    Lead Developer
    Lead Developer

    Try wrapping &laquo; %link in a span: <span class="next">&laquo; %link</span>

    Same with: <span class="next">%link &raquo;</span>


    Done. THX Tom, you’re a gem!!!!!!

    A few tweaks but works perfect for me now… Here’s the final 2 lines (single quote spots were critical):

    c2c_previous_or_loop_post_link( $format='<span class="previous">%link</span>', $link='next &raquo;', $in_same_term = true, $excluded_terms = '', $taxonomy = 'category' );
    c2c_next_or_loop_post_link( $format='<span class="next">%link</span>', $link='&laquo; previous', $in_same_term = true, $excluded_terms = '', $taxonomy = 'category' );

    And here’s the css to style the arrows and next/previous names:

    .previous a{float: right;color:orange;}
    .next a{float: left;color:orange;}
    Lead Developer
    Lead Developer

    Awesome! Thanks for sharing the code 🙂


    Just want to say that I tried the firt Tom’s snippet and it worked instantly!
    with no tweak! Thanks!!!

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