从一个技巧小马的代码修改而来,因为它不适用于我当前版本的WP(3.5.1)
添加了虚线类,因为WP现在既包括下划线版本的页面层次结构类,也包括虚线版本的页面层次结构类
更改了array_diff->array_intersect,因为diff返回所有类,而不是过滤列表
// for custom menus
add_filter(\'nav_menu_css_class\', \'normalize_wp_classes\', 10, 2);
// for the page menu fallback (wp_list_pages)
add_filter(\'page_css_class\', \'normalize_wp_classes\', 10, 2);
/**
* @param $classes array
* @param $item object
* @return array
*/
function normalize_wp_classes($classes, $item){
// old class => new class
$replacements = array(
\'current-menu-item\' => \'active\',
\'current-menu-parent\' => \'active-parent\',
\'current-menu-ancestor\' => \'active-parent\',
\'current_page_item\' => \'active\',
\'current_page_parent\' => \'active-parent\',
\'current_page_ancestor\' => \'active-parent\',
\'current-page-item\' => \'active\',
\'current-page-parent\' => \'active-parent\',
\'current-page-ancestor\' => \'active-parent\'
);
// do the replacements above
$classes = strtr(implode(\',\', $classes), $replacements);
$classes = explode(\',\', $classes);
// remove any classes that are not present in the replacements array,
// and return the result
return array_unique(array_intersect(array_values($replacements), $classes));
}