- This topic has 15 replies, 1 voice, and was last updated 5 years, 2 months ago by
twarrior.
-
AuthorPosts
-
January 31, 2018 at 9:26 pm #485032
freeform1999
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!!!
Rand=================
PS – FWIW I have a feeling the following might be the approach
generate_category_post_navigation
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; }January 31, 2018 at 11:31 pm #485099Tom
Lead DeveloperLead DeveloperHi 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 🙂
January 31, 2018 at 11:38 pm #485106freeform1999
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!!!February 1, 2018 at 9:35 am #485598Tom
Lead DeveloperLead DeveloperThe 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?
February 1, 2018 at 10:21 am #485663freeform1999
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!!!!
February 1, 2018 at 1:22 pm #485868freeform1999
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.)
THX!!!February 1, 2018 at 9:40 pm #486098Tom
Lead DeveloperLead DeveloperTough 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
February 1, 2018 at 10:50 pm #486154freeform1999
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!:
http://coffee2code.com/wp-plugins/loop-post-navigation-links
https://wordpress.org/plugins/loop-post-navigation-linksFebruary 2, 2018 at 10:04 am #486567Tom
Lead DeveloperLead DeveloperAwesome! Thanks for sharing those plugins! 🙂
February 2, 2018 at 11:06 am #486656freeform1999
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 withc2c_next_or_loop_post_link( $format='« %link', $link='previous', $in_same_term = true, $excluded_terms = '', $taxonomy = 'category' ); c2c_previous_or_loop_post_link( $format='%link »', $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 »', $link='%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) c2c_previous_or_loop_post_link( $format='« %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.THX!!!!!!
RandFebruary 2, 2018 at 9:23 pm #486908Tom
Lead DeveloperLead DeveloperTry wrapping
« %linkin a span:<span class="next">« %link</span>Same with:
<span class="next">%link »</span>February 2, 2018 at 11:33 pm #486946freeform1999
Yeaaaaaa!
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 »', $in_same_term = true, $excluded_terms = '', $taxonomy = 'category' ); c2c_next_or_loop_post_link( $format='<span class="next">%link</span>', $link='« 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;}February 3, 2018 at 9:50 am #487262Tom
Lead DeveloperLead DeveloperAwesome! Thanks for sharing the code 🙂
March 25, 2020 at 6:46 am #1209466Daniel
Hi!
Just want to say that I tried the firt Tom’s snippet and it worked instantly!
with no tweak! Thanks!!!March 25, 2020 at 9:45 am #1209836Tom
Lead DeveloperLead DeveloperGreat to hear 🙂
-
AuthorPosts
- You must be logged in to reply to this topic.