WordPress, シングルでもwp_list_categoriesにcurrent-cat

参考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');

新カレントメニューの作り方 画像置き換え含む

ヘッダー

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
<script type="text/javascript" src="js/jquery-1.7.2.min.js"></script>
 
<!--画像カレントホバー-->
<script type="text/javascript" >
$(function(){
 
 $(".d_navi_main_in li:not(.current) a").each(function(){
  var a = $(this);
  var img = a.find("img");
  var src_off = img.attr("src");
  var src_on = src_off.replace(/^(.+)_off(\.[^\.]+)$/,"$1_on$2");
 
  $("<img />").attr("src",src_on);
 
  a.bind("mouseenter focus", function(){
        img.attr("src", src_on);
        });
 
  a.bind("mouseleave blur", function(){
        img.attr("src", src_off);
        });
    });
});
 
 //current表示
$(function() {
  if ($('.d_navi_main_in li').hasClass('current')) {
 $(".d_navi_main_in li.current a img").attr("src",$(".d_navi_main_in li.current a img").attr("src").replace(/^(.+)_off(\.[^\.]+)$/,"$1_current$2"));
  }
});
</script>
1
2
3
4
5
6
7
8
#d_sidenavi_in ul li.current a {
    color: #71B5FF!important;
    text-decoration: none!important;
    border-top-color: #71B5FF!important;
    border-right-color: #71B5FF!important;
    border-bottom-color: #71B5FF!important;
    border-left-color: #71B5FF!important;
}
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
<!--メインナビ-->
<div class="d_navi_main_all">
  <div class="d_navi_main_width">
    <div class="d_navi_main_in">
<ul>
  <li <?php if(is_home()): ?> class="current"<?php endif; ?>>
  <a href="<?php echo home_url(); ?>"><img src="img/main_menu/menu_put-01_off.jpg" width="192" height="70" /></a></li>
   
  <li <?php if(is_page( '21' )): ?> class="current"<?php endif; ?>>
  <a href="<?php echo home_url(); ?>/?page_id=21"><img src="img/main_menu/menu_put-02_off.jpg" width="213" height="70" /></a></li>
   
  <li <?php if(is_page( '19' ) or is_category() or in_category(array(1,2) and is_single())): ?> class="current"<?php endif; ?>>
  <a href="<?php echo home_url(); ?>/?page_id=19"><img src="img/main_menu/menu_put-03_off.jpg" width="199" height="70" /></a></li>
   
  <li <?php if(is_page( '23' )): ?> class="current"<?php endif; ?>>
  <a href="<?php echo home_url(); ?>/?page_id=23"><img src="img/main_menu/menu_put-04_off.jpg" width="194" height="70" /></a></li>
   
  <li <?php if(is_page( '25' )): ?> class="current"<?php endif; ?>>
  <a href="<?php echo home_url(); ?>?page_id=25"><img src="img/main_menu/menu_put-05_off.jpg" width="202" height="70" /></a></li>
   
   
</ul>
    </div class="d_navi_main_in">
  </div class="d_navi_main_width">
</div class="d_navi_main_all">
<!--/メインナビ-->
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
41
     <ul>
 
<li <?php if(is_page( '28' )): ?> class="current"<?php endif; ?>>
<a href="<?php echo home_url(); ?>/?page_id=28">理事長ご挨拶</a></li>
 
<li <?php if(is_page( '30' )): ?> class="current"<?php endif; ?>>
<a href="<?php echo home_url(); ?>/?page_id=30">メンバー紹介</a></li>
 
<li class="space"></li>
<li class="dash"></li>
<li class="space"></li>
  
 
 <li <?php if(is_category(1) or in_category(array(1) and is_single())): ?> class="current"<?php endif; ?>>
 <a href="<?php echo home_url(); ?>/?cat=1">お知らせ</a></li>
  
<li <?php if(is_category(2) or in_category(array(2) and is_single())): ?> class="current"<?php endif; ?>>
<a href="<?php echo home_url(); ?>/?cat=2">活動報告</a></li>
 
<li class="space"></li>
<li class="dash"></li>
<li class="space"></li>
    <li <?php if(is_page( '32' )): ?> class="current"<?php endif; ?>>
    <a href="<?php echo home_url(); ?>/?page_id=32">リンク</a></li>
     
<li <?php if(is_page( '62' )): ?> class="current"<?php endif; ?>>
<a href="<?php echo home_url(); ?>/?page_id=62">公開情報</a></li>
 
 
 
<li class="space"></li> <li class="space"></li>
 
<li class="kyuujc"><a href="http://setojcnews.jugem.jp/" target="_blank">旧JC NEWS</a></li>
     
 
      <li class="space"></li> <li class="space"></li>
       
       <li class="member<?php if(is_page( '64' )): ?> current<?php endif; ?>" ><a href="<?php echo home_url(); ?>/?page_id=64"> メンバーページ</a></li>
       
       
    </ul>