KnpMenuBuilder dropdownlist in menu (Bootstrap) - php

In my current web application I am making use of KnpMenuBuilder and Bootstrap to make a menu. I want to add an unordered list to a list item element, so that it appears as a dropdown menu, just like the example(s) given in the Bootstrap documentation. For this I should also add a dropdown-menu class to this <ul></ul> tag. It should look something like this:
<ul>
<li>
...
</li>
<li><!-- or this should be <li class="dropdown"> not entirely sure
if this happens with javascript or something yet -->
<a href="#" class="dropdown-toggle">
Dropdown
</a>
<ul class="dropdown-menu">
<li>Dropdown menu item 1</li>
<li>Dropdown menu item 2</li>
</ul>
</li>
</ul>
I know you can add such element in the MenuBuilder class:
public function mainMenu(FactoryInterface $factory, array $options)
{
$menu = $factory->createItem('root', array(
'childrenAttributes' => array(
'class' => 'nav navbar-nav',
),
));
$menu->addChild('Home', array('route' => 'home'));
$menu->addChild('Movies', array('route' => 'movies'))
->setAttribute('class', 'dropdown');
$menu['Movies']->addChild('Add a movie', array('route' => 'movie_create_form'));
//This renders <li>movies<ul><li>Add a movie</li></ul></li>
//I want to add the drop-down class to the <ul></ul> tag
return $menu;
}
My current result:
<!-- Ofcourse <ul> tag should be around this aswell, but my question is not about the beauty of this code -->
<li>...</li>
<li dropdown="dropdown" class="active last">
<a href="#">
Dropdown
</a>
<ul class="menu_level_1">
<li class="first last">
<a href="#">
Dropdown menu item 1
</a>
</li>
</ul>
</li>
Or: http://pastebin.com/XHyYeWer
(original: http://pastebin.com/gnK2G0uz)
How should I change my (builder class) code?
Thanks in advance.

Related

my Css code for navigation menu not working in Wordpress theme

while making my custom navigation menus for my own Worpress theme, navigation menu's css code's some codes are working, some are not working while entering php navigation code for Wordpress theme.
This my php code for wordpress navigation menu
<?php wp_nav_menu( array(
'theme_location' => 'primary',
'depth' => 2,
'container' => 'ul',
'container_class' => '',
'menu_class' => 'nav-link',
'fallback_cb' => 'WP_Bootstrap_Navwalker::fallback',
'walker' => new WP_Bootstrap_Navwalker())
);?>
so i am having so many classes in navigation menu so my css code is very long that's why i am adding my html code for my navigiation bar. this my html code for navigation menu.
<ul>
<li class="nav-link" style="--i: .6s">
Home
</li>
<li class="nav-link" style="--i: .85s" >
Categories<i class="fas fa-caret-down"></i>
<div class="dropdown ">
<ul>
<li class="dropdown-link">
Motivation
</li>
<li class="dropdown-link">
Health
</li>
<li class="dropdown-link">
<a class=" href="/pages/science&tech.php">Science & Technology</a>
</li>
<li class="dropdown-link">
<a href='/pages/finance.php'>Finance</a>
</li>
<li class="dropdown-link">
Random
</li>
<div class="arrow"></div>
</ul>
</div>
</li>
<li class="nav-link" style="--i: 1.1s">
Archives<i class="fas fa-caret-down"></i>
<div class="dropdown">`enter code here`enter code here`
<ul>
<li class="dropdown-link">
Contact us
</li>`enter code here`
<li class="dropdown-link">
Privacy Policy
</li>
<li class="dropdown-link">
Terms & Condition
</li>
<li class="dropdown-link">
Disclaimer
</li>
<div class="arrow"></div>
</ul>
</div>
</li>
<li class="nav-link" style="--i: 1.35s">
About
</li>
</ul>
something wrong in my php code. can you give correct code for my navigation bar.
We don't see you code in context, but your 'container' element has wrong. You must set a nav or div, not ul (i suggest nav tag). Remember define 'container_class' too.
Then you must define your CSS rules including your container_class in spreadsheet. If is necessary, you must use !important.

how to convert static HTML5 navigation menu to wordpress dynamic menu

How to convert static HTML5 navigation menu to WordPress dynamic menu.
I want to output this HTML menu in WordPress.and I don't know how to make ul and li in this form.
Thanks for your help.
This is my HTML5 navigation menu
<ul class="nav topnav">
<li class="dropdown active">
<i class="icon-home"></i> Home <i class="icon-angle-down"></i>
<ul class="dropdown-menu">
<li>Homepage 2</li>
<li>Homepage 3</li>
<li>Parallax slider</li>
<li>Landing page</li>
</ul>
</li>
<li class="dropdown">
Features <i class="icon-angle-down"></i>
<ul class="dropdown-menu">
<li>Typography</li>
<li>Components</li>
<li>Icons</li>
<li>Icon variations</li>
<li class="dropdown">3rd menus<i class="icon-angle-right"></i>
<ul class="dropdown-menu sub-menu-level1">
<li>Sub menu</li>
<li>Sub menu</li>
</ul>
</li>
</ul>
</li>
<li class="dropdown">
Pages <i class="icon-angle-down"></i>
<ul class="dropdown-menu">
<li>About us</li>
<li>FAQ</li>
<li>Team</li>
<li>Services</li>
<li>Pricing boxes</li>
<li>404</li>
</ul>
</li>
<li class="dropdown">
Portfolio <i class="icon-angle-down"></i>
<ul class="dropdown-menu">
<li>Portfolio 2 columns</li>
<li>Portfolio 3 columns</li>
<li>Portfolio 4 columns</li>
<li>Portfolio detail</li>
</ul>
</li>
<li class="dropdown">
Blog <i class="icon-angle-down"></i>
<ul class="dropdown-menu">
<li>Blog left sidebar</li>
<li>Blog right sidebar</li>
<li>Blog fullwidth</li>
<li>Post left sidebar</li>
<li>Post right sidebar</li>
</ul>
</li>
<li>
Contact
</li>
</ul>
</nav>
I want to convert it to WordPress menu with
wp_nav_menu()
How can I make this custom menu?
I don't want to use any plugins to make custom menu.
First, you need to register nav menu in functions.php like this:
function register_menu(){
register_nav_menu('main-menu', 'Main menu');
}
add_action('init', 'register_menu');
When you do this, new option will appear in your admin panel. Go to Appearance > Menus and start adding links, pages or whatever you want to be in your menu. You can specify depth as well. Once you are done with your menu, select location of the menu, which will in this case be your newly created 'Main menu'. Now you want to go back to your page template in code editor to display that menu. It is very hard to use wp_nav_menu() function to display such complex menu so we will use different approach. We will grab menu items for start:
$menuLocation = get_nav_menu_locations();
$mainMenuID = $menuLocation['main-menu'];
$topMenuItems = wp_get_nav_menu_items($mainMenuID);
Now goes the main part. We are looping through menu items and displaying it's data in combination with custom html elements:
<?php
$menuLocation = get_nav_menu_locations();
$mainMenuID = $menuLocation['main-menu'];
$topMenuItems = wp_get_nav_menu_items($mainMenuID);
// check if menu has items
if(!empty($topMenuItems)){
?>
<ul>
<?php
foreach ($topMenuItems as $topMenuItem){
//Check if menu_item_parent property is set to 0.
That means that menu item is top level item.
if($topMenuItem->menu_item_parent == 0){
$topItemID = $topMenuItem->ID;
$submenuItems = array();
//check if there are submenu items for current top menu
item.
foreach ($topMenuItems as $submenuItem){
if($submenuItem->menu_item_parent == $topItemID){
$submenuItems[] = $submenuItem;
}
}
?>
<li>
<?php echo $topMenuItem->title ?>
<?php
// If there are submenu items for current item, display
them.
if(!empty($submenuItems)){
?>
<ul class="submenu">
<?php
foreach ($submenuItems as $subitem){
?>
<li>
<?php echo $subitem->title ?>
</li>
<?php
}
?>
</ul>
<?php
}
?>
</li>
<?php
}
}
?>
</ul>
<?php
}
?>
// Using this logic, you can go in depth how much you want to.

Menu WordPress get nav menu item

I have the following html structure:
<ul class="nav navbar-nav navbar-right navbar-fw">
<li class="dropdown">Home
<ul class="dropdown-menu">
<li>coach
</li>
<li>speaker
</li>
<li>training
</li>
<li>online content
</li>
<li>coming soon
</li>
<li>corporate
</li>
</ul>
I would like to use wp_get_nav_menu_bar to display the items of my WordPress menu.
For now use this and it's a list with my items
<?php wp_nav_menu( array( 'theme_location' => 'Top' ) ); ?>

Edit HTML of a wordpress menu

i want to change the default HTML of wordpress's nav in a my custom theme adding a caret in the li that has a sub menu.
The default html looks like:
<ul id="menu-menu" class="nav navbar-nav">
<li id="menu-item-98" class="classes">Normal item</li>
<li id="menu-item-105" class="classes menu-item-has-children dropdown">Has a sub menu
<ul class="sub-menu">
<li id="menu-item-113" class="classes">Sub menu item</li>
</ul>
</li>
</ul>
And I havo to change this in this form:
<ul id="menu-menu" class="nav navbar-nav">
<li id="menu-item-98" class="classes">Normal item</li>
<li id="menu-item-105" class="classes menu-item-has-children dropdown">
<a href="#">
Has a sub menu
<!-- ADD THIS PART -->
<span class="caret"></span>
</a>
<ul class="sub-menu">
<li id="menu-item-113" class="classes">Sub menu item</li>
</ul>
</li>
</ul>
I think that it's possible do something like this with php in function.php but I don't know how. Can you help me? Thanks in advice.
Add your submenu under Appearance --> Menus.
Then, you can use the Chrome/Firebug inspector to find the class/id of the UL for the submenu(s) you would like to add the caret to. Modify your child theme's style.css with something like the following:
ul .sub-menu {
list-style-image: url('caret.gif');
}

Add a class to a menu item in WordPress

I am creating a theme in WordPress. I want to add a class to a <li> element if the user is on that page.
I have managed to create a dynamic navigation, using following code in my header.php:
<div class="nav">
<?php wp_nav_menu( array( 'theme_location' => 'nav-menu' ) ); ?>
</div>
Which in HTML, translates to:
<div class="nav">
<ul>
<li> Home </li>
<li> Products </li>
<li> About </li>
</ul>
</div>
I am hoping to dynamically alter this. If the user is on www.website.com/about, the navigation will change to:
<div class="nav">
<ul>
<li> Home </li>
<li> Products </li>
<li class="underline"> About </li>
</ul>
</div>
According to the docs, the current page item should already have a class .current-menu-item. you can use that to style the item with underline. Do you not see this class?
http://codex.wordpress.org/Function_Reference/wp_nav_menu#Current-Page_Menu_Items

Categories