- This topic has 17 replies, 3 voices, and was last updated 4 years, 1 month ago by Yannick.
-
AuthorPosts
-
February 10, 2020 at 7:02 am #1160676Yannick
Hi!
How can I change the post navigation order? See URL provided.
At the bottom, it displays the previous post as “Cuisine 9” and the next to “Cuisine 7”.
It should be the other way around (7 should be previous, and 9 next). I’ve tried playing withget_adjacent_post
, but no success…February 10, 2020 at 8:29 am #1160770DavidStaffCustomer SupportHi there,
Maybe alphabetizing the main query would be the easiest method:
February 10, 2020 at 9:59 am #1160870YannickNot very helpful, as I already change the way the posts are ordered with
add_filter('posts_orderby', ..);
, like so:global $wpdb; return "LENGTH($wpdb->posts.post_title) ASC, $wpdb->posts.post_title ASC"; return $orderby;
Note that I use LENGTH for natural sorting, to avoid sorting like so: Cuisine 1, Cuisine 10, Cuisine 2…
February 10, 2020 at 3:23 pm #1161108TomLead DeveloperLead DeveloperTricky one..
Try adding this PHP:
add_filter( 'generate_post_navigation_args', function( $args ) { $args['previous_format'] = '<div class="nav-next">' . generate_get_svg_icon( 'arrow' ) . '<span class="next" title="' . esc_attr__( 'Next', 'generatepress' ) . '">%link</span></div>'; $args['next_format'] = '<div class="nav-previous">' . generate_get_svg_icon( 'arrow' ) . '<span class="prev" title="' . esc_attr__( 'Previous', 'generatepress' ) . '">%link</span></div>'; return $args; } );
February 10, 2020 at 3:48 pm #1161126YannickYep, that works. Didn’t think of this logic ๐
Just need to figure out how to have the navigation also sort naturally (because of numbers in titles). So I see “Cuisine 1”, “Cuisine 10”, “Cuisine 11”, “Cuisine 2”, “Cuisine 3”, … (note that it sorts fine on the blog/home page).
February 11, 2020 at 4:37 am #1161625YannickTom: any easy way to switch how they are displayed? Right now, the “next” is above the “previous” (I want it the other way around)…
February 11, 2020 at 5:49 am #1161720DavidStaffCustomer SupportIf you’re ok with CSS to re-order them then this:
.site-main .post-navigation { display: flex; flex-direction: column-reverse; }
February 11, 2020 at 6:02 am #1161730YannickI always prefer PHP as it is more robust than CSS, but I’ll use this while I wait for Tom.
February 11, 2020 at 8:54 am #1162092TomLead DeveloperLead DeveloperIf you need PHP, you would have to overwrite the entire function, which would be a shame. In this case, I think CSS is the preferred method ๐
February 11, 2020 at 9:03 am #1162102YannickThank Tom. Any idea how I can take into consideration the “NATURAL” sorting for the post navigation?
As I mentionned above, I’m using the following to order posts:
global $wpdb;
return “LENGTH($wpdb->posts.post_title) ASC, $wpdb->posts.post_title ASC”;
return $orderby;Works well on archives, blog/home page (sorts like so: Cuisine 1, Cuisine 2, Cuisine 3 and not Cuisine 1, Cuisine 10, Cuisine 2, Cuisine 3…), but the post navigation doesn’t seem to take this into consideration. So at the bottom of Cuisine 2, I get Cuisine 1 (previous) and Cuisine 20 (next), when it should be Cuisine 3.
February 11, 2020 at 4:04 pm #1162369TomLead DeveloperLead DeveloperWhere are you adding that code?
What if you do this instead?:
add_action( 'pre_get_posts', function( $query ) { if ( $query->is_main_query() && ! is_admin() ) { $query->set( 'orderby', 'title' ); $query->set( 'order', 'ASC' ); } } );
February 11, 2020 at 4:06 pm #1162370YannickIt has to be sorted by title, and take into consideration natural sorting (https://en.wikipedia.org/wiki/Natural_sort_order)… I’m a programmer, and I know what needs to be done, but I can’t figure out where it needs to be called, or what hook/filter I need to use.
Like I said, it’s sorted properly on the blog/home page, in the archives, but not for the post navigation…
February 11, 2020 at 4:27 pm #1162374TomLead DeveloperLead DeveloperIf it’s possible, you’d need to alter the actual query, which my code above does: https://generatepress.com/forums/topic/change-the-post-navigation-order/#post-1162369
I’m not 100% sure if it will work, but that query should alter every query on the site to orderby title.
February 11, 2020 at 4:28 pm #1162376YannickYeah. The problem is the natural sorting issue…
I would get C1, C10, C11, C2… and not C1, C2, …, C10, C11.February 11, 2020 at 4:35 pm #1162380TomLead DeveloperLead DeveloperPerhaps this post will help?: https://wordpress.stackexchange.com/a/73194
The post navigation is using those functions, so your answer is likely there if you update the query to match what you’re after.
-
AuthorPosts
- You must be logged in to reply to this topic.