mpn
mpn

Reputation: 1018

Custom Order for Categories in Wordpress

I would like to use the get_categories() function in wordpress to display categories in a specific custom order. There seems to be no easy way to do this.

This is my current code:

        $cat_order = array(26,31,30,35,34,37,36,33,38,28,32,29,27);
        $category_args = array(
            'order'                    => 'ASC',
            'include'                  => $cat_order,
        ); 
        $categories = get_categories( $category_args ); 

For some reason I cannot order them by the specified order in the array. Is this even possible? It is possible for posts.

Upvotes: 0

Views: 218

Answers (1)

Stickers
Stickers

Reputation: 78686

Yes, you can.

<?php
    $my_categories = array(26,31,30,35,34,37,36,33,38,28,32,29,27);

    echo '<ul>';
    foreach($my_categories as $my_category) {
        $category_args = array(
            'include' => $my_category,
        ); 
        $categories = get_categories($category_args);

        foreach($categories as $cat) {
            echo '<li>'.$cat->cat_ID.'</li>';
        }
    }
    echo '</ul>';
?>

More details http://codex.wordpress.org/Function_Reference/get_categories

Upvotes: 1

Related Questions