Skip to content

Instantly share code, notes, and snippets.

@ann71727
Last active November 5, 2017 20:26
Show Gist options
  • Save ann71727/3cb87f33c50de29107d8bee0cbabbcd0 to your computer and use it in GitHub Desktop.
Save ann71727/3cb87f33c50de29107d8bee0cbabbcd0 to your computer and use it in GitHub Desktop.
wp_nav_menu() 修改或自訂義文字及類別
<?php
/**
* WordPress modify wp_nav_menu text and classes
* https://v123.tw
*/
$args = array(
'theme_location' => 'main-menu',
'container' => 'v123-nav',
'container_id' => 'v123-menu-menu',
'menu_class' => 'v123-menu-div',
'items_wrap' => '<ul id="%1$s" class="%2$s">%3$s</ul>',
'depth' => 5,
'link_before' => '<span>',
'link_after' => '</span>',
);
wp_nav_menu( $args );
/**
* Custom Nav Text
* https://v123.tw
*/
add_filter('nav_menu_item_title' , 'v123_nav_text' , 10 , 4 );
function v123_nav_text($title, $item, $args, $depth){
if(array_search('menu-item-has-children',$item->classes)){
$title.='<i class="icon-plus"></i>';
}
return $title;
}
/**
* Custom Nav Classes
* https://v123.tw
*/
add_filter('nav_menu_css_class' , 'v123_nav_class' , 10 , 2 );
function v123_nav_class ($classes, $item) {
if (in_array('current-menu-item', $classes) ){
$classes[] = 'active ';
}
return $classes;
}
/**
* 相關 hook 可參考
* https://developer.wordpress.org/reference/classes/walker_nav_menu/
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment