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 have problem that my sub-menu is missing when I use (tried built in categories also) custom categories as menu items.

Basically what I want:

Menu item (wordpress page) -submenu item what is custom taxonomy
--sub categories
--sub categories
--sub categories
-submenu item what is custom taxonomy
--sub categories

Now I can get the menu to work with Menus. What I want is that my submenu is open when I am inside sub categories page.

Submenu item actually should open first sub category.

I am using custom walker for my menu design and now I am thinking that is there something missing that it will close submenu when on custom taxonomy sub category page.

class submenu_Walker extends Walker_Nav_Menu {

  function walk( $elements, $max_depth) {

    global $wp_query;
    $current_menu = (int) $wp_query->queried_object_id;

    $tops = array(); // top level menu items
    $subs = array(); // sub menu items

    _wp_menu_item_classes_by_context($elements);

    foreach ($elements as $element) {
      if (0 == $element->menu_item_parent) {
        $tops[] = $element;
        if ($element->object_id == $current_menu) {
          $current_parent = $element;
        }
      }
      else
      {
        $subs[] = $element;

        if ($element->object_id == $current_menu) {
          $current_child = $element;

        }
      }
    }

    if (!$current_parent)

      $current_parent = $this->get_current_parent_from_sub($elements, $current_child);

    if ($current_parent) {
      $real_subs = array();

      foreach ($subs as $item) {
        if ($item->menu_item_parent == $current_parent->db_id)
          $real_subs[] = $item;
      }
    }

    // show top level elements

    $s = '<nav class="navigation"><ul class=first>';

    foreach ($tops as $item) {
      $s .= $this->display_item($item);
    }

    $s .= "</ul></nav>";

    // show sub menu items

    if (sizeof($real_subs) > 0) {
      $s .= "<nav class='navigation sub'><div class='strt'></div><div class='horizontal_content'><ul class='first'>";
      foreach ($real_subs as $item) {
        $s .= $this->display_item($item);
      }
      $s .= '</ul></div><div class="finito"></div></nav>';
    }
    return $s;
  }

  // get the parent page when a sub page is the current page

  function get_current_parent_from_sub($elements, $current_child) {

    foreach ($elements as $element) {
      if ($element->db_id == $current_child->menu_item_parent) {
        return $element;
      }
    }
    return false;
  }

  function display_item($item) {

    $i = '';
    $this->start_el( $i, $item, 0, array() );
    $this->end_el( $i, $item, 0, array() );

    return $i;

  }
}
share|improve this question
add comment

Your Answer

 
discard

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

Browse other questions tagged or ask your own question.