Skip to content

Instantly share code, notes, and snippets.

@JodiWarren
Last active December 12, 2015 06:38
Show Gist options
  • Save JodiWarren/4730111 to your computer and use it in GitHub Desktop.
Save JodiWarren/4730111 to your computer and use it in GitHub Desktop.
Subnav Menu Wordpress
#subnav {
li {
list-style-type: none;
}
a {
padding-left: 1em;
padding-right: 1em;
display: block
}
.menu-primary-container {
> .menu {
margin-left: 0;
}
}
.menu {
//Top-level subnav items
> .sub-menu {
margin-left: 0;
> li {
background: #f2f2f2;
&.current-menu-parent, &.current-menu-item {
background-color: #126090;
> a {
color: white;
}
ul {
display: block;
}
&:hover{
> a{
color: white;
}
}
}
&:hover{
a {
color: #126090;
}
}
> a {
text-transform: uppercase;
color: #aaa;
font-weight: 300;
padding-top: .2em;
padding-bottom: .2em;
}
//Submenu within subnav
.sub-menu {
display: none;
margin-left: 0;
li {
padding-left: 1em;
background: white;
&:first-child{
padding-top: .6em;
}
&:last-child {
padding-bottom: .2em;
}
// text-transform: none;
a {
color: black;
}
&.current-menu-item {
position: relative;
&:before {
content: ">";
position: absolute;
left: 1em;
}
a {
color: #126090;
font-weight: 700;
}
}
&:hover {
color: #126090;
&:before {
content: ">";
position: absolute;
left: 1em;
}
}
}
}
}
}
}
}
// ********************
// Coopa Sub Nav Walker
// ********************
class sub_nav_walker extends Walker_Nav_Menu {
var $found_parents = array();
function start_el(&$output, $item, $depth, $args) {
global $wp_query;
//this only works for second level sub navigations
$item_output = "";
$parent_item_id = 0;
$indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
$class_names = $value = '';
$classes = empty( $item->classes ) ? array() : (array) $item->classes;
$class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item ) );
$class_names = ' class="' . esc_attr( $class_names ) . '"';
#current_page_item
// Checks if the current element is in the current selection
if (strpos($class_names, 'current-menu-item')
|| strpos($class_names, 'current-menu-parent')
|| strpos($class_names, 'current-menu-ancestor')
|| (is_array($this->found_parents) && in_array( $item->menu_item_parent, $this->found_parents )) ) {
// Keep track of all selected parents
$this->found_parents[] = $item->ID;
//check if the item_parent matches the current item_parent
if($item->menu_item_parent!=$parent_item_id){
$output .= $indent . '<li id="menu-item-'. $item->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>';
//$item_output .= '</li>';
$item_output .= $args->after;
}
//}
$output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
}
}
function end_el(&$output, $item, $depth) {
// Closes only the opened li
if (strpos($class_names, 'current-menu-item')
|| strpos($class_names, 'current-menu-parent')
|| strpos($class_names, 'current-menu-ancestor')
|| (is_array($this->found_parents) && in_array( $item->menu_item_parent, $this->found_parents )) ) {
$output .= "</li>\n";
}
}
function end_lvl(&$output, $depth) {
$indent = str_repeat("\t", $depth);
// If the sub-menu is empty, strip the opening tag, else closes it
//if($_SERVER['REMOTE_ADDR']=="81.187.108.150")echo("<pre>".print_r("---".trim($output)."---",1)."</pre>");
if (substr($output, -22)=="<ul class=\"sub-menu\">\n") {
// if($_SERVER['REMOTE_ADDR']=="81.187.108.150")echo("<pre>".print_r("empty",1)."</pre>");
$output = substr($output, 0, strlen($output)-23);
} else {
// if($_SERVER['REMOTE_ADDR']=="81.187.108.150")echo("<pre>".print_r("closing",1)."</pre>");
$output .= "$indent</ul>\n";
}
$output .= "";
$output = preg_replace('/>\s\s+</', '><', $output);
$output = str_replace('<ul class="sub-menu"></ul>','',$output);
}
}
// **(((((((((())))))))))**
// End Coopa Sub Nav Walker
// **(((((((((())))))))))**
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment