I have this problem that I am encountering. I am trying to sort my categories to display in the order that I want them to. I have read the WordPress documentation on all of the sort options. However, there is no inclination of how to choose the categories and make them flow the way I want.
This is what the Codex presents:
orderby (string) Sort categories alphabetically or by unique category ID. The default is sort by Category ID. Valid values:
- id
- name - default
- slug
- count
- term_grouporder (string) Sort order for categories (either ascending or descending). The default is ascending. Valid values:
- asc - default
- desc
However, like I said, this does not help me because I need them to display in the order I choose.
Here is the code I am implementing at the moment. Which is in the order that I want them to display.
<?php
$args = array(
'orderby' => 'ID',
'order' => 'ASC',
'include' => '5,6,7,8,29,9,10,11,12,13,14,15,16'
);
$categories = get_categories($args);
foreach($categories as $category) {
echo '<li><a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name . '</a>' . ' ' . '(' . $category->count . ')' . '</li> ';
}
?>
I have this problem that I am encountering. I am trying to sort my categories to display in the order that I want them to. I have read the WordPress documentation on all of the sort options. However, there is no inclination of how to choose the categories and make them flow the way I want.
This is what the Codex presents:
orderby (string) Sort categories alphabetically or by unique category ID. The default is sort by Category ID. Valid values:
- id
- name - default
- slug
- count
- term_grouporder (string) Sort order for categories (either ascending or descending). The default is ascending. Valid values:
- asc - default
- desc
However, like I said, this does not help me because I need them to display in the order I choose.
Here is the code I am implementing at the moment. Which is in the order that I want them to display.
<?php
$args = array(
'orderby' => 'ID',
'order' => 'ASC',
'include' => '5,6,7,8,29,9,10,11,12,13,14,15,16'
);
$categories = get_categories($args);
foreach($categories as $category) {
echo '<li><a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name . '</a>' . ' ' . '(' . $category->count . ')' . '</li> ';
}
?>
Share
Improve this question
edited Aug 7, 2013 at 21:20
Mayeenul Islam
12.9k21 gold badges85 silver badges169 bronze badges
asked Aug 7, 2013 at 19:18
Wayne Hatter Jr.Wayne Hatter Jr.
1671 gold badge2 silver badges11 bronze badges
3
- This post might help wphub/sorting-categories-custom-sort-order – Srikanth AD Commented Aug 7, 2013 at 19:30
- Thank you for that link. I looked at it but it seems like a lot of work and I don't want to really use a plugin either. But I thank you very much for responding so quickly. I am really grateful for the time you took to try and help. – Wayne Hatter Jr. Commented Aug 7, 2013 at 19:56
- This might help. Although it's for posts but the second solution could be useful for you as it is using php and array. – Sisir Commented Aug 7, 2013 at 20:01
3 Answers
Reset to default 9If you're already using Advanced Custom Fields (which you should be!) you can create an order field for your categories from which you can set them manually in numerical order.
Then all you have to do is:
$categories = get_categories( $args );
usort($categories, function($a, $b) {
return get_field("category_order", "category_".$a->term_id) - get_field("category_order", "category_".$b->term_id);
});
foreach ($categories as $category){
...
Where category_order
is the field name you created with ACF.
Note: Using the PHP 5.3 way of usort.
Okay, I found this plugin that does exactly what I want. I didn't want to use a plugin but this one seemed pretty light weight and it did the job perfectly. http://wordpress/plugins/taxonomy-terms-order/
The original code will work if you change the arguments to: 'orderby' => 'include'
$args = array(
'orderby' => 'include',
'order' => 'ASC',
'include' => '5,6,7,8,29,9,10,11,12,13,14,15,16'
);
$categories = get_categories($args);
foreach($categories as $category) {
echo '<li><a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name . '</a>' . ' ' . '(' . $category->count . ')' . '</li> ';
}