WordPress.org

Ready to get started?Download WordPress

Forums

Add page ID to walker function start_lvl (8 posts)

  1. R Bouten
    Member
    Posted 1 year ago #

    Hi,

    I am trying to make the Twitter Bootstrap accordion menu to work in WordPress. Therefore I need to get the page ID in my walker function start_lvl to put it in the <ul> tag so it can get triggered.

    This is what I am trying to achieve in HTML:

    <li id="" class="accordion-group">
    <a href="#collapsePAGEID" class="active" data-toggle="collapse" data-parent="#menu">Menu Option 1</a>
    	<ul id="collapsePAGEID" class="accordion-body in collapse">
    		<li id="" class="active">
    			<a href="" class="">Submenu Option 1</a>
    			etc...
    		</li>
    	</ul>
    </li>

    I used start_el to put the page ID in <a href="#collapsePAGEID">. But I can't use start_lvl in the same way.

    My code so far for start_lvl:

    // add classes to ul sub-menus
    function start_lvl( &$output, $depth, $item ) {
        // depth dependent classes
        $indent = ( $depth > 0  ? str_repeat( "\t", $depth ) : '' ); // code indent
        $display_depth = ( $depth + 1); // because it counts the first submenu as 0
        $pgid = ; //How to get the page ID?
        $ids = array(
        	'collapse' . $pgid
        	);
        $id_name = implode( ' ', $ids );
    
        // build html
        $output .= "\n" . $indent . '<ul id="' . $id_name . '" >' . "\n";
    }

    Any clues?? Much appreciated!

  2. keesiemeijer
    moderator
    Posted 1 year ago #

    Try it with this [untested]:

    global $post;
    $pgid = $post->ID; //How to get the page ID?
  3. R Bouten
    Member
    Posted 1 year ago #

    Hi Keessie,

    Thank you for your help. There is an ID being displayed now in the right place. Unfortunately it's not the right ID yet.... It's the ID of the current page where the menu is displayed not the ID of the page where the walker is walking true. Or how do I explain.... the ID connected to the <ul> tag.

    I found an example with the functionality I need for start_el (where they use $item->ID , but I can't get it implemented the same way in start_lvl.

    // add main/sub classes to li's and links
     function start_el( &$output, $item, $depth, $args ) {
        global $wp_query;
        $indent = ( $depth > 0 ? str_repeat( "\t", $depth ) : '' ); // code indent
    
        // depth dependent classes
        $depth_classes = array(
            ( $depth == 0 ? 'accordion-group main-menu-item' : 'sub-menu-item' ),
            ( $depth >=2 ? 'sub-sub-menu-item' : '' ),
            ( $depth % 2 ? 'menu-item-odd' : 'menu-item-even' ),
            'menu-item-depth-' . $depth
        );
        $depth_class_names = esc_attr( implode( ' ', $depth_classes ) );
    
        // passed classes
        $classes = empty( $item->classes ) ? array() : (array) $item->classes;
        $class_names = esc_attr( implode( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item ) ) );
    
        // build html
        $output .= $indent . '<li id="nav-menu-item-'. $item->ID . '" class="' . $depth_class_names . ' ' . $class_names . '">';
    
        // link attributes
        $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        ) .'"' : '';
        $attributes .= ! empty( $item->url )        ? ' href="'   . esc_attr( $item->url        ) .'#collapse'.$item->ID.'"' : '';
        $attributes .= ' data-toggle="collapse" ' . ( $depth == 0 ? 'data-parent="#woningmenu"' : 'data-parent="#"' ) . '';
        $attributes .= ' class="menu-link ' . ( $depth > 0 ? 'sub-menu-link' : 'main-menu-link' ) . '"';
    
        // build html
        $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args ); } }
  4. keesiemeijer
    moderator
    Posted 1 year ago #

    Is this a custom walker for a wp_nav_menu?

  5. R Bouten
    Member
    Posted 1 year ago #

    Yes, it's for wp_nav_menu

  6. Ryan Burnette
    Member
    Posted 1 year ago #

    I'm wrestling with this now. Did anyone ever find a solution?

  7. Ryan Burnette
    Member
    Posted 1 year ago #

  8. R Bouten
    Member
    Posted 1 year ago #

    Hi thnx for the link, in the end I solved in a dirty way for the project I was doing and afterwards never got around redoing it properly. I will have a look at the link. Also here http://wordpress.stackexchange.com/questions/62054/custom-walker-how-to-get-id-in-function-start-lvl/ some solutions where proposed later. Think they are quiet good.

    Cheers

Topic Closed

This topic has been closed to new replies.

About this Topic