記事ごとにカスタムタクソノミーを表示させた時に並び順を一覧表示と同じにする方法
全部のタームを出力し並び順を任意で並べたい場合、「wp_list_categories」関数を使えば一覧をリストで表示し、プラグインで設定した並び順で並び替えてくれます。
ただ、single.phpなどでその記事に設定しているタームだけの一覧を表示する場合は、「wp_list_categories」を使うと全タームが出力されてしまう為、「wp_get_post_terms」関数を使うことになります。引数の記事のIDを渡せれるので、その記事に設定されたタームだけを取得することが可能です。
手順
まず、Functions.phpに以下を記載
<?php // functions.php に追記 function terms($a, $b){ if ( intval($a->term_order) == intval($b->term_order)) { return 0; } return (intval($a->term_order) < intval($b->term_order)) ? -1 : 1; } function terms_sort($terms, $object_ids, $taxonomies, $args){ if(!is_admin()){ usort($terms , "terms"); } return $terms; } add_filter('wp_get_object_terms','terms_sort',99,4); ?>
出力させたい箇所に記載
<ul> <?php $lists = wp_get_post_terms($post->ID,'タクソノミー名',array( 'orderby'=>'order', 'order'=>'ASC' ) ); ?> <?php foreach($lists as $list): ?> <li><a href="<?php echo esc_url(get_term_link($list,'タクソノミー名')); ?>" title="<?php echo esc_attr($list->name); ?>"><?php echo esc_html($list->name); ?></a></li> <?php endforeach; ?>