Forum Replies Created

  • In reply to: different sec. menu's per page

    #22648

    I have hacked a bit today and chosen a very different approch to yours, and I believe I have found a much more elegant solution.

    You can see it in action on http://espressomd.org/wordpress/ (some minutes after posting this)

    My child theme only requires some code in “functions.php” (see below).

    When this is used, the secondary menu will simply show the second level of your main menu.

    This solves some of the problems with your approach:

    • The shadow below the top-level menu
    • The hard-coded list of pages (just use the main menu)

    I think I will ask the author whether he wants to include the code into the theme as a possible option.

    Here comes the code:

    /* removing this filter removes the ancestor class stuff */
    add_action( 'after_setup_theme', 'graphene_child_theme_setup' );
    function graphene_child_theme_setup() {
    remove_filter( 'nav_menu_css_class', 'graphene_add_ancestor_class', 2, 10);
    }

    /* this filter sets the depth of the header menu to 1 */
    add_filter('graphene_header_menu_args', 'graphene_child_set_depth_to_one' );
    function graphene_child_set_depth_to_one($args) {
    $args['depth'] = 1;
    return $args;
    }

    /* this filter adds the SubMenu_Walker and sets the depth to 1 */
    add_filter('graphene_secondary_menu_args', 'graphene_child_set_submenu_walker' );
    function graphene_child_set_submenu_walker($args) {
    $args['depth'] = 2;
    $args['theme_location'] = 'Header Menu';
    $args['walker'] = new SubMenu_Walker();
    return $args;
    }

    class SubMenu_Walker extends Walker_Nav_Menu {
    function __construct($min_depth = 1) {
    $this->min_depth = $min_depth;
    $this->in_current_submenu = false;
    }

    function start_el( &$output, $item, $depth, $args ) {
    if ($depth == $this->min_depth-1)
    $this->in_current_submenu = $item->current_item_parent || $item->current;
    else if ($depth >= $this->min_depth && $this->in_current_submenu)
    parent::start_el( $output, $item, $depth - $this->min_depth, $args );
    }
    function end_el( &$output, $item, $depth, $args ) {
    if ($depth >= $this->min_depth && $this->in_current_submenu)
    parent::end_el( $output, $item, $depth - $this->min_depth, $args );
    }

    function start_lvl( &$output, $depth, $args) {
    if ($depth >= $this->min_depth && $this->in_current_submenu)
    parent::start_lvl( $output, $depth - $this->min_depth, $args);
    }
    function end_lvl( &$output, $depth, $args) {
    if ($depth >= $this->min_depth && $this->in_current_submenu) {
    $this->in_current_element = false;
    parent::end_lvl( $output, $depth - $this->min_depth, $args);
    }
    }

    }

    In reply to: different sec. menu's per page

    #22646

    Nice what you have done, and I will steal it for our site!

    Do I assume correct that you hooked into the “graphene_top_menu” hook? If so, how did you get rid of the menu shadow below it?

    And, just as an idea: wouldn’t it be possible to use the page hierarchy to specify for which pages which secondary menu is shown? Then you wouldn’t have to specify a hard-coded list in the code.

    And another issue: If I see it right, when you choose a page in the secondary menu, then the corresponding top-level menu is not selected anymore. Any way around that?

Viewing 2 posts - 1 through 2 (of 2 total)