Next / Previous Post Links: Alphabetically and from same Category
-
I’m exhaustively searching for a method to provide different Next and Previous Links at Single Posts.
By DEFAULT it:
- Is chronological ordered
- Links to posts from all blog categories
But I NEED it:
- Alphabetically ordered
- Linking to posts from same category only
I found this code that let me now order the Next/Prev Links alhpabetically however I still can’t show links only from same category of the post.
function filter_next_post_sort($sort) { if (get_post_type($post) == 'post') { $sort = "ORDER BY p.post_title ASC LIMIT 1"; } else{ $sort = "ORDER BY p.post_date ASC LIMIT 1"; } return $sort; } function filter_next_post_where($where) { global $post, $wpdb; if (get_post_type($post) == 'post') { return $wpdb->prepare("WHERE p.post_title > '%s' AND p.post_type = '". get_post_type($post)."' AND p.post_status = 'publish'",$post->post_title); } else{ return $wpdb->prepare( "WHERE p.post_date > '%s' AND p.post_type = '". get_post_type($post)."' AND p.post_status = 'publish'", $post->post_date, $post->post_type ); } } function filter_previous_post_sort($sort) { if (get_post_type($post) == 'post') { $sort = "ORDER BY p.post_title DESC LIMIT 1"; } else{ $sort = "ORDER BY p.post_date DESC LIMIT 1"; } return $sort; } function filter_previous_post_where($where) { global $post, $wpdb; if (get_post_type($post) == 'post') { return $wpdb->prepare("WHERE p.post_title < '%s' AND p.post_type = '". get_post_type($post)."' AND p.post_status = 'publish'",$post->post_title); } else{ return $wpdb->prepare( "WHERE p.post_date < '%s' AND p.post_type = '". get_post_type($post)."' AND p.post_status = 'publish'", $post->post_date, $post->post_type ); } } add_filter('get_next_post_sort', 'filter_next_post_sort'); add_filter('get_next_post_where', 'filter_next_post_where'); add_filter('get_previous_post_sort', 'filter_previous_post_sort'); add_filter('get_previous_post_where', 'filter_previous_post_where');
Note: Every post is assigned to only one category.
THANK YOU FOR HELP ME!
Viewing 6 replies - 1 through 6 (of 6 total)
Viewing 6 replies - 1 through 6 (of 6 total)
- The topic ‘Next / Previous Post Links: Alphabetically and from same Category’ is closed to new replies.