My client has dragged around the sub-categories in the admin panel to change their order.
I have the following code that outputs the sub-categories of a specific category parent:
$mainCategory = get_categories(
array (
'parent' => $category_id['term_id'],
'taxonomy' => 'product_cat',
'orderby' => 'menu_order'
)
);
foreach( $mainCategory as $mc ) {
$cat_link = get_category_link( $mc->term_id );
echo '<div class="main-category col-md-3">';
echo '<a href="'.$cat_link.'">';
$thumbnail_id = get_woocommerce_term_meta( $mc->term_id, 'thumbnail_id', true ); // Get Category Thumbnail
$image = wp_get_attachment_url( $thumbnail_id );
if ( $image ) {
echo '<div class="mc-img">';
echo '<img src="' . $image . '" alt="" />';
echo '</div>';
}
echo '<h2>';
echo $mc->name;
echo '</h2>';
echo '<p>';
echo $mc->description;
echo '</p>';
echo '<div class="mc-button">Explore Products</div>';
echo '</a>';
echo '</div>';
}
The categories are output in alphabetical order, rather than the order specified by the admin in the dashboard.
'menu_order' does not work
My client has dragged around the sub-categories in the admin panel to change their order.
I have the following code that outputs the sub-categories of a specific category parent:
$mainCategory = get_categories(
array (
'parent' => $category_id['term_id'],
'taxonomy' => 'product_cat',
'orderby' => 'menu_order'
)
);
foreach( $mainCategory as $mc ) {
$cat_link = get_category_link( $mc->term_id );
echo '<div class="main-category col-md-3">';
echo '<a href="'.$cat_link.'">';
$thumbnail_id = get_woocommerce_term_meta( $mc->term_id, 'thumbnail_id', true ); // Get Category Thumbnail
$image = wp_get_attachment_url( $thumbnail_id );
if ( $image ) {
echo '<div class="mc-img">';
echo '<img src="' . $image . '" alt="" />';
echo '</div>';
}
echo '<h2>';
echo $mc->name;
echo '</h2>';
echo '<p>';
echo $mc->description;
echo '</p>';
echo '<div class="mc-button">Explore Products</div>';
echo '</a>';
echo '</div>';
}
The categories are output in alphabetical order, rather than the order specified by the admin in the dashboard.
'menu_order' does not work
Share Improve this question asked Oct 19, 2016 at 4:43 AlexnlAlexnl 3071 gold badge6 silver badges16 bronze badges3 Answers
Reset to default 3Answering for posterity, since I ran into the same issue and this thread is the closest thing I found to what I was looking for.
The solution turned out to be quite simple.
If you don't specify 'orderby' in your arguments, the default sort order will be used (the category order in the Woo admin). Just remove that part of your arguments.
Why don't you use the Navigational Menu in Wordpress. This way your client can add new terms to the custom menu in the order they so choose it will be reflected as such on your page.
Create a new custom menu in your functions.php
file,
function register_my_menu() {
register_nav_menu('product-list-menu',__( 'Product Listing' ));
}
add_action( 'init', 'register_my_menu' );`
Then go to your dashboard menu section, Appearance->Menu, and add your custom taxonomy terms or sub-category terms in the order you wish to list them. (Here is a tutorial if you need more info).
Finally you can display your menu on your page as a list, using
<?php wp_nav_menu( array( 'theme_location' => 'product-list-menu' ) ); ?>
the wp_nav_menu
function (codex documentation) allows you to add your own css class to custom style your list.
Wordpress uses a Walker class to build menus. Building each item as a list. You can hook into the default Walker class, using the nav_menu_link_attributes
(codex documentation) to customise the html attributes for each menu item, and the wp_nav_menu_items
to add custom items at the end of the menu list (tutorial).
If you want even more control over the way your list is created (for example use <h2>
tags for your term names, then use a custom Walker class which you can parse to the function. Having your own Walker class can make for rather powerful functionality, for example if new terms are added to your taxonomy and these have not been added to your menu, then you can verify this at the time of building the list, allowing you to add new items which have not been ordered at the end of the list.
Hope this helps.
I am adding another answer here for your information which is quite different from the first one I provided, hence for clarity I am putting it as 2nd answer.
You can use a plugin to order terms within taxonomies. I cam across the Custom Taxonomy Order New Edition in an article recently which I am yet to try. Searching on google I also came across the Category Order and Taxonomy Terms Order which seems to be doing something similar. You might want to give these a spin to see if they meet your needs.