参考https://www.evernote.com/pub/0xmxkx/mynotebook#st=p&n=c1dcd2ca-de1c-46d6-a3fd-2171181c40dc
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | //カテゴリ シングルでもwp_list_categories にカレント function sgr_show_current_cat_on_single( $output ) { global $post ; if ( is_single()) { $categories = wp_get_post_categories( $post ->ID); foreach ( $categories as $catid ) { $cat = get_category( $catid ); // Find cat-item-ID in the string if (preg_match( '#cat-item-' . $cat ->cat_ID . '#' , $output )) { $output = str_replace ( 'cat-item-' . $cat ->cat_ID, 'cat-item-' . $cat ->cat_ID . ' current-cat' , $output ); } } } return $output ; } add_filter( 'wp_list_categories' , 'sgr_show_current_cat_on_single' ); カスタム //タクソノミーd_cate_jirei ポストタイプd_jirei シングルでもwp_list_categories にカレント function d_show_current_tram_on_single( $output ) { global $post ; if ( is_singular( 'd_jirei' )) { $tarms =wp_get_post_terms( $post ->ID, 'd_cate_jirei' ); foreach ( $tarms as $tarm ) { $tarmid = $tarm ->term_id; $dtarm = get_term( $tarmid , 'd_cate_jirei' ); // Find cat-item-ID in the string if (preg_match( '#cat-item-' . $dtarm ->term_id . '#' , $output )) { $output = str_replace ( 'cat-item-' . $dtarm ->term_id, 'cat-item-' . $dtarm ->term_id . ' current-cat' , $output ); } } } return $output ; } add_filter( 'wp_list_categories' , 'd_show_current_tram_on_single' ); |