Skip to content

Instantly share code, notes, and snippets.

@rwsite
Last active November 19, 2023 22:32
Show Gist options
  • Save rwsite/5e8ded9ef2f1534dab22a7e31e9d798c to your computer and use it in GitHub Desktop.
Save rwsite/5e8ded9ef2f1534dab22a7e31e9d798c to your computer and use it in GitHub Desktop.
get woocommerce categories sorting by order
<ul>
<?php $term_children = get_term_children( get_queried_object()->term_id, 'product_cat' ); ?>
<?php if($term_children) {
$sorted=[];
foreach ( $term_children as $child ) {
$child_category = get_term_by( 'id', $child, 'product_cat' );
$meta = get_term_meta($child_category->term_id,'order', true);
$child_category->position = intval($meta);
$sorted[$meta] = $child_category;
}
uasort($sorted, function ($a, $b) {
return $a->position > $b->position;
});
foreach ($sorted as $child){
?>
<li><a href="<?php echo esc_url(get_term_link( $child->term_id )); ?>"><?php echo esc_html($child->name); ?></a></li>
<?php
}
}
?>
</ul>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment