Breaking

Followers

Tuesday, 16 January 2018


We can use third level category in menubar in wordpress.




$thiscat =  get_query_var('cat'); // The id of the current category
  $catobject = get_category($thiscat,false); // Get the Category object by the id of current category
  if($thiscat)
   $topcat = get_top_parent_cat($thiscat);
  
  $parentcat = $catobject->category_parent;
  global $wp_query;
  $page_title = $wp_query->post->post_title;
  $current_dest_menu = 'level-3';
  if($page_title=='Destinations' || $topcat==158)   
   $current_dest_menu = 'level-3 current-menu-item';
  
  $custom_menu = '<li class="'.$current_dest_menu.'"><a href="'.get_bloginfo("url").'/destinations/">Destinations</a>
            <ul class="sub-menu">';
   
   $categories = get_categories('&orderby=name&order=ASC&child_of=158&hide_empty');
   $count =1;
   $x = 0;
   foreach($categories as $category) { 
   
   if ($category->category_parent ==158){
    $countchildren = count (get_term_children( $category->cat_ID, 'category' ));
    
    if($parentcat==$category->cat_ID){
     if($countchildren>1)
      $menu_class_name= "col-3-level current-menu-ancestor";
     else
      $menu_class_name= "col-2-level current-menu-ancestor"; 
    } else{
     if($countchildren>1)
      $menu_class_name= "col-3-level";
     else
      $menu_class_name= "col-2-level";
    }
    
     $custom_menu .=' <li class="'.$menu_class_name.'"><a href="'.get_category_link($category->cat_ID).'">'.$category->name.'</a><ul>';
     
     
       $args = array(                                                  
           'orderby' => 'name',     
           'hide_empty' => true,
           'child_of' =>$category->cat_ID
           );

         $child_categories = get_categories($args); 
        foreach($child_categories as $category) { 
         if($thiscat==$category->cat_ID) 
          $custom_menu .='<li class="current-menu-item"><a href="'.get_category_link($category->cat_ID).'">'.$category->cat_name.'</a></li>';
         else
          $custom_menu .='<li><a href="'.get_category_link($category->cat_ID).'">'.$category->cat_name.'</a></li>'; 

        }
    $custom_menu .='</ul></li>' ;       
            }} 
           $custom_menu .='</ul></li>';
          
    wp_nav_menu( array(
     'menu'=>'Main Navigation',
     'container_class'   => 'mainMenu align-right',     
     'items_wrap'     => '<ul id="menu-main-navigation" class="menu">'.$custom_menu.'%3$s</ul>'
    ) );
    

No comments:

Post a Comment