Skip to content

Instantly share code, notes, and snippets.

@Ikaring
Forked from tkc49/gist:5625795
Last active August 29, 2015 14:23
Show Gist options
  • Save Ikaring/4a760193a17c78235c08 to your computer and use it in GitHub Desktop.
Save Ikaring/4a760193a17c78235c08 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() && isset( $terms[0]->term_order )){
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>
@Ikaring
Copy link
Author

Ikaring commented Jun 16, 2015

タームの順番は問題なかったけど、wp_nav_menu()を使っている他のところで以下のエラーが出てたので、issetで対処
Notice: Trying to get property of non-object
Warning: usort(): Array was modified by the user comparison function

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment