Reputation:
I am calling a list of Woocommerce categories and am trying to get them to show in the custom, darg-and-drop, order that they have been organised in but to no avail. The usual 'orderby' => 'menu_order' isn't working. Code below:
<?php
$args=array(
'orderby' => 'menu_order',
'order' => 'ASC',
'child_of' => 13,
'hide_empty' => 0,
'taxonomy' => 'product_cat'
);
$categories=get_categories($args);
foreach($categories as $category) {
echo "<li class='filter-option " . $category->slug . "'><a href='#' data-filter-value='." . $category->slug . "'>";
echo $category->name;
echo "</a></li>";
}
?>
Any help would be greatly appreciated
Upvotes: 2
Views: 9769
Reputation: 686
The parameter you are looking for the get_categories() arguments is
'menu_order' => 'asc'
so eg. my function for returning first three root Woocommerce categories with excluding the uncategorized looks like this:
// load first three categories from Woocommerce
function my_get_woocommerce_categories() {
$args = array(
'taxonomy' => 'product_cat',
'parent' => 0,
'hide_empty' => false,
'number' => 3,
'show_uncategorized' => false,
'menu_order' => 'asc',
);
$categories = get_categories( $args );
return $categories;
}
Upvotes: 8
Reputation: 9
Please try this...
<?php $wcatTerms = get_terms('product_cat', array('hide_empty' => 0, 'orderby' => 'ASC', 'parent' =>0)); //, 'exclude' => '17,77'
foreach($wcatTerms as $wcatTerm) :
$wthumbnail_id = get_woocommerce_term_meta( $wcatTerm->term_id, 'thumbnail_id', true );
$wimage = wp_get_attachment_url( $wthumbnail_id );
?>
<ul>
<li class="libreak"><?php if($wimage!=""):?><img src="<?php echo $wimage?>"><?php endif;?></li>
<li>
<a href="<?php echo get_term_link( $wcatTerm->slug, $wcatTerm->taxonomy ); ?>"><?php echo $wcatTerm->name; ?></a>
<ul class="wsubcategs">
<?php
$wsubargs = array(
'hierarchical' => 1,
'show_option_none' => '',
'hide_empty' => 0,
'parent' => $wcatTerm->term_id,
'taxonomy' => 'product_cat'
);
$wsubcats = get_categories($wsubargs);
foreach ($wsubcats as $wsc):
?>
<li><a href="<?php echo get_term_link( $wsc->slug, $wsc->taxonomy );?>"><?php echo $wsc->name;?></a></li>
<?php
endforeach;
?>
</ul>
</li>
</ul>
<?php
endforeach;
?>
Upvotes: 0