Created
January 4, 2014 15:32
-
-
Save WebEndevSnippets/8256521 to your computer and use it in GitHub Desktop.
Genesis: Replace Genesis primary menu with a dynamically updated category menu. Uses a custom walker class.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//* Unregister primary/secondary navigation menus | |
remove_theme_support( 'genesis-menus' ); | |
//* Add New Menu Navigation with dynamic child category walker class | |
add_action( 'genesis_after_header', 'webendev_subcat_navigation', 15 ); | |
function webendev_subcat_navigation() { | |
echo '<nav class="nav-primary">'; | |
wp_nav_menu( | |
array( | |
'container_class' => 'wrap', | |
'theme_location' => 'primary', | |
'menu_class' => 'menu genesis-nav-menu menu-secondary', | |
'walker' => new Submenu_Walker_Nav_Menu() | |
) | |
); | |
echo '</nav>'; | |
} | |
add_action('wp_loaded','webendev_register_nav_menu_class'); | |
/** | |
* New walker class to extend Walker_Nav_Menu | |
* Dynamically adds child categories to menu. | |
* | |
*/ | |
function webendev_register_nav_menu_class(){ | |
class Submenu_Walker_Nav_Menu extends Walker_Nav_Menu { | |
function start_el(&$output, $item, $depth, $args) { | |
global $wp_query; | |
$indent = ( $depth ) ? str_repeat( "\t", $depth ) : ''; | |
$class_names = $value = ''; | |
$classes = empty( $item->classes ) ? array() : (array) $item->classes; | |
$classes[] = 'menu-item-' . $item->ID; | |
$class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item ) ); | |
$class_names = ' class="' . esc_attr( $class_names ) . '"'; | |
$id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args ); | |
$id = strlen( $id ) ? ' id="' . esc_attr( $id ) . '"' : ''; | |
$output .= $indent . '<li' . $id . $value . $class_names .'>'; | |
$attributes = ! empty( $item->attr_title ) ? ' title="' . esc_attr( $item->attr_title ) .'"' : ''; | |
$attributes .= ! empty( $item->target ) ? ' target="' . esc_attr( $item->target ) .'"' : ''; | |
$attributes .= ! empty( $item->xfn ) ? ' rel="' . esc_attr( $item->xfn ) .'"' : ''; | |
$attributes .= ! empty( $item->url ) ? ' href="' . esc_attr( $item->url ) .'"' : ''; | |
$item_output = $args->before; | |
$item_output .= '<a'. $attributes .'>'; | |
$item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after; | |
$item_output .= '</a>'; | |
if($item->object == 'category'){ | |
$child_cats = wp_list_categories('title_li=&echo=0&hide_empty=0&child_of='.$item->object_id); | |
if( $child_cats ){ | |
$item_output .= '<ul class="sub-menu">' .$child_cats. '</ul>'; | |
} | |
} | |
$item_output .= $args->after; | |
$output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args ); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment