Reputation: 1
A template I'm using is calling the_category()
to list the categories which a post belongs. It's ordering them by name. Is there a way to reverse the order by ID category?
Upvotes: 0
Views: 2758
Reputation: 146191
Just replace your the_category(' ')
with this
$cats=get_the_category();
$cid=array();
foreach($cats as $cat) { $cid[]=$cat->cat_ID; }
$cid=implode(',', $cid);
foreach((get_categories('orderby=name&include='.$cid)) as $category) { // notice orderby
echo '<a href="'.get_category_link($category->cat_ID).'">'.$category->cat_name.'</a> '; // keep a space after </a> as seperator
}
You can use orderby value one of the following
id
name - default
slug
count
term_group
Upvotes: 4
Reputation: 15550
Currently the_category hasn't order by functionality. You need to go to wp-includes/category-template.php There is a function called "get_the_terms" in that file.In this function there is a line like $terms = wp_get_object_terms( $id, $taxonomy );
wp_get_object_terms can take extra argument for args.You can edit here liike below;
$args = array('orderby' => 'id', 'order' => 'ASC');
$terms = wp_get_object_terms( $id, $taxonomy, $args);
You can modify $args according to your needs
Hope this helps
Upvotes: 0