
A very common problem with WordPress sites is that you can’t easily reaarange category order. Sure, you can use a custom menu, but for some sites you have to reaarrange the order of your categories. This snippet will show you how to do it easily.

A very common problem with WordPress sites is that you can’t easily reaarange category order. Sure, you can use a custom menu, but for some sites you have to reaarrange the order of your categories. This snippet will show you how to do it easily.
The code above will look for a specific category (Don't forget to update line 5) and will replace it on top of all other categories. The code has to be pasted in your functions.php file.
<?php
$categories = get_terms('books_category');
for( $i=0; $i<sizeof($categories); $i++ ){
if ( $categories[$i]->name == 'New Publications' ) :
$latest = array($categories[$i]);
unset($categories[$i]);
endif;
}
if( isset($latest) )
array_splice( $categories, 0, 0, $latest );
?>
Thanks to Sebastian for the great hack!
5 Responses
Thanks for this code. I’ve been wanting to find a solution for rearranging the order of categories for awhile but forgot about it. I”l try this code in my custom functions in my child theme.
Oh, damn. Thank you very much for this code – I really needed it.
Would be very nice if you could link to the original post
@Sebastian: Done, sorry I thought wpsnippet were the original.
You can use the category ID. It you change the category name the code will break.
Trackbacks: