Alphabetising related posts
-
Hello,
I’m trying to alphabetise related posts. I’m using this code here to display related posts only from a category the pages belong to, which is output as a list:
function jp_only_rp_in_same_category( $categories, $post_id ) { $category_objects = get_the_category( $post_id ); if ( ! empty( $categories ) ) { $categories = array_merge( 'categories', 'category_objects' ); return $categories; } else { return $category_objects; } } add_filter( 'jetpack_relatedposts_filter_has_terms', 'jp_only_rp_in_same_category', 10, 2 );
This is how it looks:
Could you suggest whether if the best way of alphabetising this list is via PHP or Javascript/JQuery? I have tried using Javascript/JQuery with no luck.
I used this as a base: https://codepen.io/gabrieleromanato/pen/Cfpdu
Modified as such:
(function( $ ) { $(function() { $( "span", ".jp-relatedposts-post" ).sort(function( a, b ) { return $( a ).text() > $( b ).text(); }).appendTo( ".jp-relatedposts-post" ); }); })( jQuery );
Code is not my strength so I might have taken the wrong approach. Any help would be appreciated!
All the best,
Michael
Viewing 4 replies - 1 through 4 (of 4 total)
Viewing 4 replies - 1 through 4 (of 4 total)
- The topic ‘Alphabetising related posts’ is closed to new replies.