T
T
there42018-05-16 11:35:03
WordPress
there4, 2018-05-16 11:35:03

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

How can I set the output of the list to be sorted alphabetically?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Max Medar, 2018-05-16
@MedVedar

Use the sort functions. usort , for example.

I
Igor Vorotnev, 2018-06-27
@HeadOnFire

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 question

Ask a Question

731 491 924 answers to any question