WordPress menu and bootstrap tab intergration

790 views Asked by At

I am building a WordPress blog with bootstrap, I have used the wp_bootstrap_navwalker php to enable my wordpress menus to pass into my bootstrap code like so

 <div id="navbar" class="collapse navbar-collapse" style="background-color: #333;">
    <div class="container">
        <ul class="nav navbar-nav">
            <?php
            /* Primary navigation */
            wp_nav_menu(array(
                    'menu' => 'Primary Menu',
                    'theme_location' => 'locations-primary',
                    'depth' => 2,
                    'container' => false,
                    'container_class'   => 'collapse navbar-collapse',
                    'container_id'      => 'bs-example-navbar-collapse-1',
                    'menu_class' => 'nav navbar-nav',
                    'fallback_cb'       => 'wp_bootstrap_navwalker::fallback',
                    'walker'            => new wp_bootstrap_navwalker())
            );
            ?>
        </ul>
    </div>
</div>

This works well, although now im trying to build a tab menu using the menu system above like so

 <div class="container">
                            <ul class="nav navbar-nav">
                                <div class="row text-center" id="tabs">
                                    <?php
                                    /* Secondary navigation */
                                    wp_nav_menu(array(
                                            'menu' => 'Secondary Menu',
                                            'theme_location' => 'locations-secondary',
                                            'depth' => 2,
                                            'container' => false,
                                            'container_class' => 'collapse navbar-collapse',
                                            'container_id' => 'bs-example-navbar-collapse-1',
                                            'menu_class' => 'nav navbar-nav',
                                            'fallback_cb' => 'wp_bootstrap_navwalker::fallback',
                                            'walker' => new wp_bootstrap_navwalker())
                                    );
                                    ?>
                                </div>
                            </ul>
                        </div>

and tab content

      <div class="row text-center" id="tabs">
            <?php get_template_part('advicecentre_bar', get_post_format());>
            <div class="tab-content">
                <div class="tab-pane active" id="tab1">
                    <?php
                    $id = 177;
                    $post = get_post($id);
                    $title = apply_filters('the_title', $post->post_title);
                    $content = apply_filters('the_content', $post->post_content);
                    echo '<h1>' . $title . '</h1><hr>';
                    echo $content;
                    ?>
                </div>

                <div class="tab-pane" id="tab2">
                    <?php
                    $id = 2;
                    $post = get_post($id);
                    $title = apply_filters('the_title', $post->post_title);
                    $content = apply_filters('the_content', $post->post_content);
                    echo '<h1>' . $title . '</h1><hr>';
                    echo $content;
                    ?>
                </div>

                <div class="tab-pane" id="tab3">
                    <?php
                    $id = 168;
                    $post = get_post($id);
                    $title = apply_filters('the_title', $post->post_title);
                    $content = apply_filters('the_content', $post->post_content);
                    echo '<h1>' . $title . '</h1><hr>';
                    echo $content;
                    ?>
                </div>
</div>
</div>

within the wordpress menu cms i have created custom links and given a URL such as URL #tab1 however I am missing data-toggle="tab"to get the tabs working, I can not see a way to do this through the CMS dose anyone know how I can get this working.

UPDATE

Ok so I am trying to edit the following code to say if menu_class is nav nav-tabs then append data-toggle="tab" in the <a> need some help doing this

public function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
        $indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';

        /**
         * Dividers, Headers or Disabled
         * =============================
         * Determine whether the item is a Divider, Header, Disabled or regular
         * menu item. To prevent errors we use the strcasecmp() function to so a
         * comparison that is not case sensitive. The strcasecmp() function returns
         * a 0 if the strings are equal.
         */
        if ( strcasecmp( $item->attr_title, 'divider' ) == 0 && $depth === 1 ) {
            $output .= $indent . '<li role="presentation" class="divider">';
        } else if ( strcasecmp( $item->title, 'divider') == 0 && $depth === 1 ) {
            $output .= $indent . '<li role="presentation" class="divider">';
        } else if ( strcasecmp( $item->attr_title, 'dropdown-header') == 0 && $depth === 1 ) {
            $output .= $indent . '<li role="presentation" class="dropdown-header">' . esc_attr( $item->title );
        } else if ( strcasecmp($item->attr_title, 'disabled' ) == 0 ) {
            $output .= $indent . '<li role="presentation" class="disabled"><a href="#">' . esc_attr( $item->title ) . '</a>';
        } else {

            $class_names = $value = '';

            $classes = empty( $item->classes ) ? array() : (array) $item->classes;
            $classes[] = 'menu-item-' . $item->ID;

            $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args ) );

            if ( $args->has_children )
                $class_names .= ' dropdown';

            if ( in_array( 'current-menu-item', $classes ) )
                $class_names .= ' active';

            $class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';

            $id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args );
            $id = $id ? ' id="' . esc_attr( $id ) . '"' : '';

            $output .= $indent . '<li' . $id . $value . $class_names .'>';

            $atts = array();
            $atts['title']  = ! empty( $item->title )   ? $item->title  : '';
            $atts['target'] = ! empty( $item->target )  ? $item->target : '';
            $atts['rel']    = ! empty( $item->xfn )     ? $item->xfn    : '';

            // If item has_children add atts to a.
            if ( $args->has_children && $depth === 0 ) {
                $atts['href']           = '#';
                $atts['data-toggle']    = 'dropdown';
                $atts['class']          = 'dropdown-toggle';
                $atts['aria-haspopup']  = 'true';
//                $atts['data-toggle'] = 'elementscroll';
            } else {
                $atts['href'] = ! empty( $item->url ) ? $item->url : '';
            }

            $atts = apply_filters( 'nav_menu_link_attributes', $atts, $item, $args );

            $attributes = '';
            foreach ( $atts as $attr => $value ) {
                if ( ! empty( $value ) ) {
                    $value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value );
                    $attributes .= ' ' . $attr . '="' . $value . '"';
                }
            }

            $item_output = $args->before;

Link to wp_bootstrap_navwalker

0

There are 0 answers