Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I hope somebody can show me some light on this. I am working on a site that has a 3 level menu which loads calling 3 different wp menus (let's say "main-menu", "projects-submenu", "2nd-level-projects-submenu", where "projects-submenu" are categories and "2nd-level-projects-submenu" are subcategories of one category in "projects-submenu". But came across one annoying bug (or one error by myself I don't seem to identify) regarding wp adding a current-menu class to the wrong menu item. This is, instead of activating the corresponding "projects" item menu when visiting one of its submenus, it activates the wrong menu item "news" adding the class current-menu-item to it wrongly.

I have checked the wp menus on the admin panel several times and also deleted and rebuilt them again but nothing seems to fix this.

Here is a link to the temporary site: http://bruto.se/wp/proyectos/equipamientos/

Code example that loads a menu:

<?php get_header(); ?>

<?php wp_nav_menu( array('menu' => 'proyectos' )); ?>


<?php
if ( is_category_or_sub(4) ) {
wp_nav_menu( array('menu' => 'proyectos-2level' ));
}
else {}
?>

Screenshots:

http://min.us/mbjE27te3W

share|improve this question
2  
Could you post some code? and maybe the screenshots of your menus from the admin page. There is no bug with wp_nav_menu and the error probably comes from not setting the three separate menus correctly. It would be much easier to have just one menu and then create a custom walker to only go down 'one branch' –  Stephen Harris Jun 16 '12 at 12:44
    
Edited. Hope the info helps –  Charliechocolate Jun 16 '12 at 17:33

1 Answer 1

For some reason (and it's not entirely clear why). The News page is considered a parent of the category page equipamientos. There's something wrong here - as the menu, sub-menu and sub-sub-menu in your code are all seperate entities, and the page 'News' and categories don't normally have any relationship. Similar your page Proyectos and its submenus including the category equipamientos - shouldn't show any relationship. What I'd expect to see is when on equipamientos, to see nothing highlighted in the first menu.

Solution

I'm not able to address the above issue, but I can suggest an alternative approach. Have only one menu - which consists of all the desired layers, but use it with a custom walker so only part of it is shown. For instance, consider this menu:

enter image description here

When on 'A' when want to view A-D and only the children of A. When on CatA1 we want to view A-D, CatA1, CatA2 and Subcat of A1. Similarly when on CatB1 we want to see A-D and CatB1.

Custom Walker

In custom walker terms as we traverse the menu, we want only the top layer (menu items with no parent), and then we want to only view items that are ancestors of the current item (and their children) and lastly the current item and its children.

This the logic behind this custom walker class:

class SH_Hideable_Sub_Nav_Walker extends Walker_Nav_Menu {  

    var $trail=array();

    // Only follow down the current branch  
    function display_element( $element, &$children_elements, $max_depth, $depth=0, $args, &$output ) {  

    $is_child_of_ancestor = in_array($element->menu_item_parent, $this->trail);

    // If not top level and not current item, an ancestor of current item or  a child of an ancestor - stop here.
    if ( 0 != $element->menu_item_parent && !$element->current && !$element->current_item_ancestor && !$is_child_of_ancestor)  
            return;

    if( $element->current || $element->current_item_ancestor )
        $this->trail[] = (int) $element->ID;

        parent::display_element( $element, &$children_elements, $max_depth, $depth, $args, &$output );  
    }  
}

The output

The HTML mark-up will be slightly different from what your theme is currently producing, so you'll need to use appropriate CSS styling to make it look how you want it to.

Example output, when on Subcat of CatA1 page:

<div class="menu-primary-container">
   <ul id="menu-primary" class="menu">

      <li id="menu-item-2704" class="menu-item menu-item-type-post_type menu-item-object-page current-menu-ancestor current_page_ancestor menu-item-2704">
         <a href="http://localhost/wordpress33/a/">A</a>

         <ul class="sub-menu">
            <li id="menu-item-2705" class="menu-item menu-item-type-taxonomy menu-item-object-category current-category-ancestor current-menu-ancestor current-menu-parent current-category-parent menu-item-2705">
               <a href="http://localhost/wordpress33/category/cata1/">CatA1</a>

               <ul class="sub-menu">
                  <li id="menu-item-2706" class="menu-item menu-item-type-taxonomy menu-item-object-category current-menu-item menu-item-2706">
                     <a href="http://localhost/wordpress33/category/cata1/subcat-of-cata1/">Subcat of CatA1</a>
                  </li>
               </ul>

            </li>
            <li id="menu-item-2707" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-2707">
               <a href="http://localhost/wordpress33/category/cata2/">CatA2</a>
            </li>
         </ul>

      </li>

      <li id="menu-item-2703" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-2703">
         <a href="http://localhost/wordpress33/b/">B</a><ul class="sub-menu"></ul>
      </li>

      <li id="menu-item-2702" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-2702">
         <a href="http://localhost/wordpress33/c/">C</a>
      </li>

      <li id="menu-item-2701" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-2701">
         <a href="http://localhost/wordpress33/d/">D</a>
      </li>
</ul>

share|improve this answer
    
I have tried your alternative solution with one single menu but the issue persists. Also the custom walker doesn't display any of the sub-menus when on a single project page. I haven't modified the CSS yet but you can see how it works at his point: –  Charliechocolate Jun 18 '12 at 14:33
    
The walker works only by showing submenus of the page you are on, or ancestors thereof. Does the single project page appear on the menu? I can see why the original issue persists though - it seems WordPress thinks the category archive page is a child of 'News'. Try it on a TwentyEleven theme and see if it works. I suspect you are changing global $post or using query_posts somewhere. –  Stephen Harris Jun 18 '12 at 14:39
    
The single project page does not appear on the menu. It displays one chosen projects of a selected category. The menu shows: pages at the top level, categories at the second level (except for the page "estudio" where the 2nd level are pages too) and subcategories at level 3. I am using global $post on the page "Proyectos" on the main level and query_posts somewhere else too. I will check that out –  Charliechocolate Jun 18 '12 at 15:46
    
All checked. No query_posts and not changing global $post anywhere. Stuck :( –  Charliechocolate Jun 18 '12 at 21:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.