Start by adding a custom field to your tag or category with Advanced Custom Fields, name it to “custom_field” or something, and add this to your functions.php.
1 2 3 | function sort_tags_by_customer_field($b, $a){ return strcmp($a->custom_field, $b->custom_field); } |
Modify the tag or category object in the loop by getting the custom field, but it back, sort it and use it how you want.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | $terms = get_the_terms( $post->ID, 'custom_tag'); if($terms){ $t = 0; foreach($terms as $term){ $custom_field = get_field('custom_field', $term); $terms[$t]->custom_field = $custom_field; $t++; } usort($terms, 'sort_tags_by_customer_field'); foreach($terms as $term){ echo '<span class="btn btn-primary btn-xs">'; echo $term->name; echo '</span>'; } } |