Site logo

Archived topic

Change the post navigation order...

17 replies · Started by Yannick on February 10, 2020

Viewing posts 1–15 of 18

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 with get_adjacent_post, but no success...

Not 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...

Tricky 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;
} );

Yep, 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).

Tom: any easy way to switch how they are displayed? Right now, the "next" is above the "previous" (I want it the other way around)...

If you're ok with CSS to re-order them then this:

.site-main .post-navigation {
    display: flex;
    flex-direction: column-reverse;
}

I always prefer PHP as it is more robust than CSS, but I'll use this while I wait for Tom.

If 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 :)

Thank 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.

Where 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' );
    }
} );

It 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...

Yeah. The problem is the natural sorting issue...
I would get C1, C10, C11, C2... and not C1, C2, ..., C10, C11.

This archived topic is closed to new replies.