Skip to content

Instantly share code, notes, and snippets.

@tkc49
Last active December 17, 2015 14:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tkc49/5625795 to your computer and use it in GitHub Desktop.
Save tkc49/5625795 to your computer and use it in GitHub Desktop.
記事毎に所属しているカスタム分類を表示した場合、カスタム分類の一覧表示の並び順と一緒の並びにする ※別途カスタム分類の並び順を制御するプラグインが必要
<?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; ?>
</ul>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment