Answer the question
In order to leave comments, you need to log in
How can I sort the list alphabetically?
Hey! Something I'm a little stuck ... I have a code that displays a list of the user's bookmarks (post titles):
/**
* Выводим список закладок
*
* @param string $post_type
* @param int $user_id
* @param int $limit
* @param bool $show_remove
*/
function display_favlist( $post_type = 'all', $user_id = false, $limit = 10, $show_remove = true ) {
$posts = $this->get_favorites( $post_type, $user_id, $limit );
echo '<div class="favclass"><ul>';
if ( $posts ) {
$remove_title = __( 'Удалить из списка, 'wl1' );
$remove_link = ' <a href="#" data-id="%s" title="%s" class="wl1-remove-favorite">x</a>';
foreach ($posts as $item) {
$extra = $show_remove ? sprintf( $remove_link, $item->post_id, $remove_title ) : '';
printf( '<li><a href="%s">%s</a>%s</li>', get_permalink( $item->post_id ), get_the_title( $item->post_id ), $extra );
}
} else {
printf( '<li>%s</li>', __( 'Нет закладок', 'wl1' ) );
}
echo '</ul></div>';
}
Answer the question
In order to leave comments, you need to log in
It is better to sort not on the output in a loop, but also when receiving all the records. In your case, this code is responsible for this:
Show what's in the get_favorites() method. Judging by the fact that you are working with an array of objects next, get_posts() is called inside this method. Ask him to sort the posts for you at the stage of sampling from the database.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question