Reputation: 633
I need order the results of "$product->get_categories()" with the slug.
The template use this code:
$cat_count = sizeof( get_the_terms( $post->ID, 'product_cat' ) );
//And after...
<?php echo $product->get_categories( ', ', '<span class="posted_in">' . _n( 'Category:', 'Categories:', $cat_count, 'woocommerce' ) . ' ', '</span>' ); ?>
I've seen in a tutorial, that I can use this code,but doesn't work (in functions.php):
function wptt_cat_order( $args ){
$args['orderby'] = 'slug';
$args['order'] = 'ASC';
return $args;
} // wptt_cat_order
add_filter( 'woocommerce_product_subcategories_args', 'wptt_cat_order' );
Other question I have is(but not is so important than the other question), why he uses the $cat_count in "_n()" function and not "get_the_terms( $post->ID, 'product_cat' )"? first is only a number O_O.
Upvotes: 3
Views: 7082
Reputation: 27102
Simple answer: you can't use that method to order the categories.
You're going to need to write your own loop using wp_get_post_terms()
, which allows you to pass arguments (such as orderby
). Something like the following should work (but I haven't tested it):
$args = array(
'orderby' => 'name',
);
$product_cats = wp_get_post_terms( $product->id, 'product_cat', $args );
$cat_count = sizeof( $product_cats );
echo '<span class="posted_in">' . _n( 'Category:', 'Categories:', $cat_count, 'woocommerce' );
for ( $i = 0; $i < $cat_count; $i++ ) {
echo $product_cats[$i]->name;
echo ( $i === $cat_count - 1 ) ? '' : ', ';
}
echo '</span>';
Upvotes: 1