Multiple Dropdown in WordPress Navigation using Bootstrap

WordPress navigation function

add this in header.php, where you want to add navigation:

 <?php
 wp_nav_menu( array(
'menu' => 'Main Navigation',
'theme_location' => 'Primary Menu',
'depth' => 3,
'container' => 'false',
'container_class' => 'collapse navbar-collapse',
'container_id' => 'bs-example-navbar-collapse',
'menu_class' => 'nav navbar-nav',
'fallback_cb' => 'wp_bootstrap_navwalker::fallback',
'walker' => new wp_bootstrap_navwalker())
);
?>

Add Below class in Functions.php in theme folder:

class wp_bootstrap_navwalker extends Walker_Nav_Menu {

 /**
 * @see Walker::start_lvl()
 * @since 3.0.0
 *
 * @param string $output Passed by reference. Used to append additional content.
 * @param int $depth Depth of page. Used for padding.
 */
 public function start_lvl( &$output, $depth = 0, $args = array() ) {
 $indent = str_repeat( "\t", $depth );
 $output .= "\n$indent<ul role=\"menu\" class=\" dropdown-menu\">\n";
 }

 /**
 * @see Walker::start_el()
 * @since 3.0.0
 *
 * @param string $output Passed by reference. Used to append additional content.
 * @param object $item Menu item data object.
 * @param int $depth Depth of menu item. Used for padding.
 * @param int $current_page Menu item ID.
 * @param object $args
 */
 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($args->has_children && $depth === 0) { $class_names .= ' dropdown'; } elseif($args->has_children && $depth > 0) { $class_names .= ' dropdown-submenu'; }

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

 // remove Font Awesome icon from classes array and save the icon
 // we will add the icon back in via a <span> below so it aligns with
 // the menu item
 if ( in_array('fa', $classes)) {
 $key = array_search('fa', $classes);
 $icon = $classes[$key + 1];
 $class_names = str_replace($classes[$key+1], '', $class_names);
 $class_names = str_replace($classes[$key], '', $class_names);

 }
 
 $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 ) {
 if ( $args->has_children ) {
 $atts['href'] = '#';
 $atts['data-toggle'] = 'dropdown';
 $atts['class'] = 'dropdown-toggle';
 } 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;

 // Font Awesome icons
 if ( ! empty( $icon ) )
 $item_output .= '<a'. $attributes .'><span class="fa ' . esc_attr( $icon ) . '"></span>&nbsp;';
 else
 $item_output .= '<a'. $attributes .'>'; 

 $item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
 $item_output .= ( $args->has_children && 0 === $depth ) ? ' <span class="caret"></span></a>' : '</a>';
 $item_output .= $args->after;

 $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
 }
 }

 /**
 * Traverse elements to create list from elements.
 *
 * Display one element if the element doesn't have any children otherwise,
 * display the element and its children. Will only traverse up to the max
 * depth and no ignore elements under that depth.
 *
 * This method shouldn't be called directly, use the walk() method instead.
 *
 * @see Walker::start_el()
 * @since 2.5.0
 *
 * @param object $element Data object
 * @param array $children_elements List of elements to continue traversing.
 * @param int $max_depth Max depth to traverse.
 * @param int $depth Depth of current element.
 * @param array $args
 * @param string $output Passed by reference. Used to append additional content.
 * @return null Null on failure with no changes to parameters.
 */
 public function display_element( $element, &$children_elements, $max_depth, $depth, $args, &$output ) {
 if ( ! $element )
 return;

 $id_field = $this->db_fields['id'];

 // Display this element.
 if ( is_object( $args[0] ) )
 $args[0]->has_children = ! empty( $children_elements[ $element->$id_field ] );

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

 /**
 * Menu Fallback
 * =============
 * If this function is assigned to the wp_nav_menu's fallback_cb variable
 * and a manu has not been assigned to the theme location in the WordPress
 * menu manager the function with display nothing to a non-logged in user,
 * and will add a link to the WordPress menu manager if logged in as an admin.
 *
 * @param array $args passed from the wp_nav_menu function.
 *
 */
 public static function fallback( $args ) {
 if ( current_user_can( 'manage_options' ) ) {

 extract( $args );

 $fb_output = null;

 if ( $container ) {
 $fb_output = '<' . $container;

 if ( $container_id )
 $fb_output .= ' id="' . $container_id . '"';

 if ( $container_class )
 $fb_output .= ' class="' . $container_class . '"';

 $fb_output .= '>';
 }

 $fb_output .= '<ul';

 if ( $menu_id )
 $fb_output .= ' id="' . $menu_id . '"';

 if ( $menu_class )
 $fb_output .= ' class="' . $menu_class . '"';

 $fb_output .= '>';
 $fb_output .= '<li><a href="' . admin_url( 'nav-menus.php' ) . '">Add a menu</a></li>';
 $fb_output .= '</ul>';

 if ( $container )
 $fb_output .= '</' . $container . '>';

 echo $fb_output;
 }
 }
}

Add this in css:

.dropdown-submenu{position:relative;}
.dropdown-submenu > .dropdown-menu{top:0;left:100%;-webkit-border-radius:0 6px 6px 6px;-moz-border-radius:0 6px 6px 6px;border-radius:0 6px 6px 6px;}
.dropdown-submenu:active > .dropdown-menu{display:block;}
.dropdown-submenu > a:after{display:block;content:" ";float:right;width:0;height:0;border-color:transparent;border-style:solid;border-width:5px 0 5px 5px;border-left-color:#cccccc;margin-top:5px;margin-right:-10px;}
.dropdown-submenu:active > a:after{border-left-color:#ffffff;}
.dropdown-submenu.pull-left{float:none;}.dropdown-submenu.pull-left > .dropdown-menu{left:-100%;margin-left:10px;-webkit-border-radius:6px 0 6px 6px;-moz-border-radius:6px 0 6px 6px;border-radius:6px 0 6px 6px;}

and add this is js file:

(function($){
 $(document).ready(function(){
 $('ul.dropdown-menu [data-toggle=dropdown]').on('click', function(event) {
 event.preventDefault(); 
 event.stopPropagation(); 
 $(this).parent().siblings().removeClass('open');
 $(this).parent().toggleClass('open');
 });
 });
})(jQuery);

Create Shortcode in PHP WORDPRESS

add_shortcode( ‘FEATUREDPOST’, ‘visitor_check_shortcode’ );
function visitor_check_shortcode($atts) {
echo ‘<h3 class=”widget-title”>Premium Biz</h3><div class=”featured-classified-ads”><ul>’;
$cartlimit = get_option(‘my-setting1’);
$limit = $cartlimit . “00”;
$sql = mysql_query(“select * from bbs_awpcp_ads where ad_item_price > ‘$limit’ and disabled = ‘0’ order by ad_id DESC LIMIT 8”);
while($ad = mysql_fetch_array($sql)){
$id = $ad[‘ad_id’];
$ss = mysql_query(“select * from bbs_awpcp_adphotos where ad_id = $id”);
$ks = mysql_fetch_array($ss);
$featuredimage = $ks[‘image_name’];
$src = ‘http://businessforbuysale.com/wp-content/uploads/awpcp/&#8217;.$featuredimage;
$noimg = “no_image.jpg”;
if (@getimagesize($src)) {
$featuredimage = $featuredimage;
}
else
{
$featuredimage = $noimg;
}
echo ‘<a href=”http://businessforbuysale.com/business/show-listing/&#8217;. $ad[‘ad_id’] .’/’. $ad[‘ad_title’].'”><li><img src=”http://businessforbuysale.com/wp-content/uploads/awpcp/&#8217; . $featuredimage .'” /><br /><p>’;
echo custom_echo($ad[‘ad_title’]);
echo “</p></li></a>”;
}
echo “</ul></div>”;

}

SHORTCODE : [FEATUREDPOST] to use

Add Navigation menu using Shortcode

Place this in Function.php
function print_menu_shortcode($atts, $content = null) {
    extract(shortcode_atts(array( 'name' => null, ), $atts));
    return wp_nav_menu( array( 'menu' => $name, 'echo' => false ) );
}
add_shortcode('menu', 'print_menu_shortcode');
then use [menu name="main-menu"] to call the menu in your content (replacing "main-menu" with your menu’s slug, of course).

Full Dynamic Twitter Bootstrap Navigation (on Hover) Function on dropdown for WordPress

Add This coding in Function.php

add_action( 'after_setup_theme', 'wpt_setup' );
    if ( ! function_exists( 'wpt_setup' ) ):
        function wpt_setup() { 
        register_nav_menu( 'primary', __( 'Primary navigation', 'wptuts' ) );
 } endif;
function wpt_register_js() {
    wp_register_script('jquery.bootstrap.min', get_template_directory_uri() . '/js/bootstrap.min.js', 'jquery');
    wp_enqueue_script('jquery.bootstrap.min');
}
add_action( 'init', 'wpt_register_js' );
function wpt_register_css() {
    wp_register_style( 'bootstrap', get_template_directory_uri() . '/css/bootstrap.css' );
    wp_enqueue_style( 'bootstrap' );
}
add_action( 'wp_enqueue_scripts', 'wpt_register_css' );
    require_once('wp_bootstrap_navwalker.php');

 

Download wp_bootstrap_navwalker From  GET HUB  add added into theme folder

Add this coding in header.php

 <?php
            wp_nav_menu( array(
                'menu'              => 'primary',
                'theme_location'    => 'primary',
                'depth'             => 2,
                'container'         => 'div',
                '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())
            );
        ?>

and remove div with collapse navbar-collapse.

To add dropdown for submenu on hover.

add

.dropdown:hover .dropdown-menu {
display: block;
}

into bootstrap.css

Our submenus are now showing on hover, but our dropdown main menu href is showing #. For showing our menu link on dropdown go to wp_bootstrap_navwalker.php file

Search for $atts[‘href’] and then replace # with $item->url;

and Remove $atts[‘data-toggle’]    = ‘dropdown’;

All is ready now.

Dynamic Twitter Bootstrap Navigation in WordPress

/**** Add this Coding bottom of function.php *****/

  1. add_action( ‘after_setup_theme’, ‘bootstrap_setup’ );
  2. if ( ! function_exists( ‘bootstrap_setup’ ) ):
  3.         function bootstrap_setup(){
  4.                 add_action( ‘init’, ‘register_menu’ );
  5.                 function register_menu(){
  6.                         register_nav_menu( ‘top-bar’, ‘Bootstrap Top Menu’ );
  7.                 }
  8.                 class Bootstrap_Walker_Nav_Menu extends Walker_Nav_Menu {
  9.                         function start_lvl( &$output, $depth ) {
  10.                                 $indent = str_repeat( “\t”, $depth );
  11.                                 $output    .= “\n$indent<ul class=\”dropdown-menu\”>\n”;
  12.                         }
  13.                         function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
  14.                                 $indent = ( $depth ) ? str_repeat( “\t”, $depth ) : ”;
  15.                                 $li_attributes = ”;
  16.                                 $class_names = $value = ”;
  17.                                 $classes = empty( $item->classes ) ? array() : (array) $item->classes;
  18.                                 $classes[] = ($args->has_children) ? ‘dropdown’ : ”;
  19.                                 $classes[] = ($item->current || $item->current_item_ancestor) ? ‘active’ : ”;
  20.                                 $classes[] = ‘menu-item-‘ . $item->ID;
  21.                                 $class_names = join( ‘ ‘, apply_filters( ‘nav_menu_css_class’, array_filter( $classes ), $item, $args ) );
  22.                                 $class_names = ‘ class=”‘ . esc_attr( $class_names ) . ‘”‘;
  23.                                 $id = apply_filters( ‘nav_menu_item_id’, ‘menu-item-‘. $item->ID, $item, $args );
  24.                                 $id = strlen( $id ) ? ‘ id=”‘ . esc_attr( $id ) . ‘”‘ : ”;
  25.                                 $output .= $indent . ‘<li’ . $id . $value . $class_names . $li_attributes . ‘>’;
  26.                                 $attributes  = ! empty( $item->attr_title ) ? ‘ title=”‘  . esc_attr( $item->attr_title ) .'”‘ : ”;
  27.                                 $attributes .= ! empty( $item->target )     ? ‘ target=”‘ . esc_attr( $item->target     ) .'”‘ : ”;
  28.                                 $attributes .= ! empty( $item->xfn )        ? ‘ rel=”‘    . esc_attr( $item->xfn        ) .'”‘ : ”;
  29.                                 $attributes .= ! empty( $item->url )        ? ‘ href=”‘   . esc_attr( $item->url        ) .'”‘ : ”;
  30.                                 $attributes .= ($args->has_children)        ? ‘ class=”dropdown-toggle” data-toggle=”dropdown”‘ : ”;
  31.                                 $item_output = $args->before;
  32.                                 $item_output .= ‘<a’. $attributes .’>’;
  33.                                 $item_output .= $args->link_before . apply_filters( ‘the_title’, $item->title, $item->ID ) . $args->link_after;
  34.                                 $item_output .= ($args->has_children) ? ‘ <b class=”caret”></b></a>’ : ‘</a>’;
  35.                                 $item_output .= $args->after;
  36.                                 $output .= apply_filters( ‘walker_nav_menu_start_el’, $item_output, $item, $depth, $args );
  37.                         }
  38.                         function display_element( $element, &$children_elements, $max_depth, $depth=0, $args, &$output ) {
  39.                                 if ( !$element )
  40.                                         return;
  41.                                 $id_field = $this->db_fields[‘id’];
  42.                                 //display this element
  43.                                 if ( is_array( $args[0] ) )
  44.                                         $args[0][‘has_children’] = ! empty( $children_elements[$element->$id_field] );
  45.                                 else if ( is_object( $args[0] ) )
  46.                                         $args[0]->has_children = ! empty( $children_elements[$element->$id_field] );
  47.                                 $cb_args = array_merge( array(&$output, $element, $depth), $args);
  48.                                 call_user_func_array(array(&$this, ‘start_el’), $cb_args);
  49.                                 $id = $element->$id_field;
  50.                                 // descend only when the depth is right and there are childrens for this element
  51.                                 if ( ($max_depth == 0 || $max_depth > $depth+1 ) && isset( $children_elements[$id]) ) {
  52.                                         foreach( $children_elements[ $id ] as $child ){
  53.                                                 if ( !isset($newlevel) ) {
  54.                                                         $newlevel = true;
  55.                                                         //start the child delimiter
  56.                                                         $cb_args = array_merge( array(&$output, $depth), $args);
  57.                                                         call_user_func_array(array(&$this, ‘start_lvl’), $cb_args);
  58.                                                 }
  59.                                                 $this->display_element( $child, $children_elements, $max_depth, $depth + 1, $args, $output );
  60.                                         }
  61.                                                 unset( $children_elements[ $id ] );
  62.                                 }
  63.                                 if ( isset($newlevel) && $newlevel ){
  64.                                         //end the child delimiter
  65.                                         $cb_args = array_merge( array(&$output, $depth), $args);
  66.                                         call_user_func_array(array(&$this, ‘end_lvl’), $cb_args);
  67.                                 }
  68.                                 //end this element
  69.                                 $cb_args = array_merge( array(&$output, $element, $depth), $args);
  70.                                 call_user_func_array(array(&$this, ‘end_el’), $cb_args);
  71.                         }
  72.                 }
  73.         }
  74. endif;

 

 

/*************Add this Coding in your header.php****************/

 

  1.  <?php
  2.                                 $args = array(
  3.                                         ‘theme_location’ => ‘top-bar’,
  4.                                         ‘depth’          => 2,
  5.                                         ‘container’      => false,
  6.                                         ‘menu_class’     => ‘nav navbar-nav’,
  7.                                         ‘walker’         => new Bootstrap_Walker_Nav_Menu()
  8.                                 );
  9.                                 wp_nav_menu($args);
  10.                         ?>