Skip to content

Instantly share code, notes, and snippets.

@tkc49
Last active December 17, 2015 10:39
Show Gist options
  • Save tkc49/5596616 to your computer and use it in GitHub Desktop.
Save tkc49/5596616 to your computer and use it in GitHub Desktop.
カスタム投稿タイプのシングルページが表示された時に任意のカスタムメニューにcurrent-menu-itemのクラスを追加する処理
<?php
function add_nav_menu_custom_class( $menu_items ) {
$lists = array(
'post_type1' => 'page_slug1',
'post_type2' => 'page_slug2',
'post_type3' => 'page_slug3',
'post_type4' => 'page_slug4',
'post_type5' => 'page_slug5',
'post_type6' => 'page_slug6',
'post_type7' => 'page_slug7',
);
$current_post_type = get_post_type();
foreach($lists as $post_type => $page_slug){
if($current_post_type==$post_type){
foreach ( $menu_items as $menu_key => $menu_item ) {
if(get_page($menu_item->object_id)->post_name == $page_slug){
$menu_items[$menu_key]->classes[] = 'current-menu-related'; //任意のクラス名
}
}
}
}
return $menu_items;
}
add_filter( 'wp_nav_menu_objects', 'add_nav_menu_custom_class' );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment