This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Use this plugin https://wordpress.org/plugins/i-order-terms/ which will let you drag/drop to re-order terms | |
// get_terms() will automatically order the terms | |
// get_the_terms() needs to be sorted afterwards. Example below. | |
function object_to_array( $object ) { | |
if ( is_array( $object ) OR is_object( $object ) ){ | |
$result = array(); | |
foreach( $object as $key => $value ){ | |
$result[$key] = object_to_array( $value ); | |
} | |
return $result; | |
} | |
return $object; | |
} | |
$terms = get_the_terms( get_the_ID(), 'some_taxonomy' ); | |
if ( $terms && ! is_wp_error( $terms ) ) : | |
// Deep convert to array | |
$terms = object_to_array( $terms ); | |
// Order by custom_order tag | |
usort( $terms, function($a, $b) { | |
return $a['custom_order'] - $b['custom_order']; | |
}); | |
foreach( $terms as $term ) { | |
echo $option['name']; | |
} | |
endif; |
Questions, comments, or suggestion for improvement? Leave a comment on the GitHub Gist page.