WordPress.org

Forums

Custom walker menu / need help with adding class (1 post)

  1. pancho_kce
    Member
    Posted 2 years ago #

    Hi, in my theme i have vertical menu (works like tree menu)
    The code look like this:

    <ul>
      <li>
      <li class="parent opened">
        <ul>
           <li>
           <li>
           <li>
           <li>
           <li class="parent">
                <ul>
                     <li>
                     <li>

    When i'am on the deepest li - li with class "parent" only don't have class opened (like this first one)

    Can't find and don't know how to modify my custom walker class. Need help with this. I think it does not provide deep levels as i need but maybe i'am wrong. Thx for help...

    My custom walker class is:

    class menu_default_walker extends Walker_Nav_Menu
    {
    
    	function start_lvl(&$output, $depth){
    		$output .= '<div><ul class="sub-menu">';
    	}
    
    	function display_element($element, &$children_elements, $max_depth, $depth=0, $args, &$output){
            $id_field = $this->db_fields['id'];
            if ( is_object( $args[0] ) ) {
                $args[0]->has_children = ! empty( $children_elements[$element->$id_field] );
            }
            return parent::display_element( $element, $children_elements, $max_depth, $depth, $args, $output );
        }
    
        function start_el(&$output, $item, $depth, $args) {
    		global $wp_query;
    		global $rb_submenus;
    
    		$indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
    
    		$new_output = '';
    		$open_class = '';
    		$depth_class = ($args->has_children ? 'parent ' : '');
    
    		$class_names = $value = '';
    		$classes = empty($item->classes) ? array() : (array) $item->classes;
    
    		$current_indicators = array('current-menu-item','current-menu-parent','current_page_item','current_page_parent');
    
    		$newClasses = array();
    		foreach($classes as $el)
    			if(in_array($el,$current_indicators))
    				array_push($newClasses,$el);
    
    		$class_names = join(' ',apply_filters('nav_menu_css_class',array_filter($newClasses),$item));
    
    		if(strpos($class_names, 'current-menu-parent') > 0 || strpos($class_names, 'current_page_parent') > 0) {
    			$class_names = ' class="' . $depth_class . $open_class . 'opened"';
    		} else if($class_names != '') {
    			$class_names = ' class="' . $depth_class . $open_class . 'selected"';
    		} else if($class_names == '') {
    			$class_names = ' class="' . $depth_class . $open_class . 'menu-item"';
    		}
    
    		if ( !get_post_meta( $item->object_id , '_members_only' , true ) || is_user_logged_in() ) {
    			$output .= $indent . '<li id="menu-item-'. $item->ID . '"' . $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        ) .'"' : '';
    
    		if($item->object != 'portfolio_category' && $item->object != 'gallery_category')
    			$attributes .= ! empty( $item->url )        ? ' href="'   . esc_attr( $item->url        ) .'"' : '';
    //--------------------------------------TP--------------------------------------
    			if($item->object != 'portfolio2_category' && $item->object != 'gallery_category')
    			$attributes .= ! empty( $item->url )        ? ' href="'   . esc_attr( $item->url        ) .'"' : '';
    			if($item->object != 'portfolio3_category' && $item->object != 'gallery_category')
    			$attributes .= ! empty( $item->url )        ? ' href="'   . esc_attr( $item->url        ) .'"' : '';
    		else
    			$attributes .= ' href="#"';
    
    		$portfolio_count;
    		$portfolio2_count;
    		$portfolio3_count;
    		$gallery_count;
    
    		if($item->object == 'portfolio_category'){
    			$terms = get_terms('portfolio_category', array('include' => $item->object_id));
    			$portfolio_count = $terms[0]->count;
    			$attributes .= ' data-category="true" data-filter="' . $terms[0]->slug .'"';
    		}
    //--------------------------------------TP--------------------------------------
    		if($item->object == 'portfolio2_category'){
    			$terms = get_terms('portfolio2_category', array('include' => $item->object_id));
    			$portfolio2_count = $terms[0]->count;
    			$attributes .= ' data-category="true" data-filter="' . $terms[0]->slug .'"';
    		}
    
    		if($item->object == 'portfolio3_category'){
    			$terms = get_terms('portfolio3_category', array('include' => $item->object_id));
    			$portfolio3_count = $terms[0]->count;
    			$attributes .= ' data-category="true" data-filter="' . $terms[0]->slug .'"';
    		}
    
    if($item->object == 'portfolio4_category'){
    			$terms = get_terms('portfolio4_category', array('include' => $item->object_id));
    			$portfolio3_count = $terms[0]->count;
    			$attributes .= ' data-category="true" data-filter="' . $terms[0]->slug .'"';
    		}
    
    		if($item->object == 'gallery_category'){
    			$terms = get_terms('gallery_category', array('include' => $item->object_id));
    			$gallery_count = $terms[0]->count;
    			$attributes .= ' data-category="true" data-filter="' . $terms[0]->slug .'"';
    		}
    
    		if($item->attr_title == 'allportfolio' || $item->attr_title == 'allportfolio2' || $item->attr_title == 'allportfolio3' || $item->attr_title == 'allportfolio4' || $item->attr_title == 'allgallery'){
    			$attributes .= ' data-all="true" data-filter="*"';
    		}
    
    		$item_output = $args->before;
    		$item_output .= '<p><a'. $attributes .'>';
    		$item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
    		$item_output .= ($item->object == 'category' ? ' ('. get_category($item->object_id)->count . ')' : '');
    		$item_output .= ($item->object == 'portfolio_category' ? ' ('. $portfolio_count . ')' : '');
    		$item_output .= ($item->object == 'portfolio2_category' ? ' ('. $portfolio2_count . ')' : '');
    		$item_output .= ($item->object == 'portfolio3_category' ? ' ('. $portfolio3_count . ')' : '');
    		$item_output .= ($item->object == 'portfolio4_category' ? ' ('. $portfolio4_count . ')' : '');
    		$item_output .= ($item->object == 'gallery_category' ? ' ('. $gallery_count . ')' : '');
    		$item_output .= ($item->attr_title == 'allblog' ? ' ('. wp_count_posts()->publish . ')' : '');
    		$item_output .= ($item->attr_title == 'allportfolio' ? ' ('. wp_count_posts('portfolio')->publish . ')' : '');
    		$item_output .= ($item->attr_title == 'allportfolio2' ? ' ('. wp_count_posts('portfolio2')->publish . ')' : '');
    		$item_output .= ($item->attr_title == 'allportfolio3' ? ' ('. wp_count_posts('portfolio3')->publish . ')' : '');
    		$item_output .= ($item->attr_title == 'allportfolio4' ? ' ('. wp_count_posts('portfolio4')->publish . ')' : '');
    		$item_output .= ($item->attr_title == 'allgallery' ? ' ('. wp_count_posts('gallery')->publish . ')' : '');
    		$item_output .= '</a></p>';
    		$item_output .= $args->after;
    
    		if ( !get_post_meta( $item->object_id, '_members_only' , true ) || is_user_logged_in() ) {
    			$output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
    		}
    
    		$output .= $new_output;
    
    	}
    
    	function end_el(&$output, $item, $depth) {
    		if ( !get_post_meta( $item->object_id, '_members_only' , true ) || is_user_logged_in() ) {
    			$output .= "</li>\n";
    		}
    	}
    
    	 function end_lvl(&$output, $depth) {
    
    		  $output .= "</ul></div>\n";
    
    	}
    
    }

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags