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.
Related
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' ) ); ?>
Sorry if this has been asked already before, but I tried looking for the answer and couldn't find it.
I am creating a website where a user submits information on their child such as age, school, skill level, etc. when registering.
Once they have completed registering their information and child information they can sign in and go to their dashboard.
I have a menu item that is called "dancers" and the sub menu item should be the dancer/child name that was entered when they registered. another menu item that is also there is "add new dancer".
So what I am trying to do is when they register, upon signing in they should already have the dancer in the dropdown menu for "dancers". which would have been retrieved from the database.
And every time they add a new dancer a new submenu item of that dancer's name should appear.
here is my html menu code:
<div class="row subnav">
<div class="col-sm-12">
<ul>
<li class="li-spacing">Home</li>
<li class="dropdown li-spacing">
<a class="dropdown-toggle" data-toggle="dropdown">Dancers<b class="caret"></b></a>
<ul class="dropdown-menu">
<li>Name</li>
<li class="divider"><hr></li>
<li>Name</li>
<li class="divider"><hr></li>
<li>Add New</li>
</ul>
</li>
<li class="dropdown li-spacing">
<a class="dropdown-toggle" data-toggle="dropdown">Report Cards <b class="caret"></b></a>
<ul class="dropdown-menu">
<li>View all</li>
<li class="divider"><hr></li>
<li>Add New</li>
</ul>
</li>
<li class="li-spacing">History</li>
<li class="li-spacing">Events</li>
<li class="li-spacing">Fun</li>
</ul>
</div>
</div>
I am very new to php, and really need some direction in where to start with this.
any help is much appreciated!
After much research and working on other pages on my website I have finally found a solution that worked perfect for me.
<div class="row subnav">
<div class="col-sm-12">
<ul>
<li class="li-spacing">Home</li>
<li class="dropdown li-spacing">
<a class="dropdown-toggle" data-toggle="dropdown">Dancers<b class="caret"></b></a>
<ul class="dropdown-menu">
<?php
$dancers = "SELECT `dancer_name` FROM `dancers` WHERE name = '$name'";
$dres = mysqli_query($con,$dancers);
if($res==FALSE){
die('there was an error running query [' . $con->error . ']');
}
while($data=mysqli_fetch_array($dres)){
echo '
<li>'.$data["dancer_name"].'</li>
<li class="divider"><hr></li>
';
}
?>
</ul>
</li>
</ul>
I have two PHP scripts:
header.php
contact_us.php
In Index.php when the user clicks the menu, it smoothly slides down.
But for the "Contact Us" page I don't want that behavior; because it is so simple, containing only a link, I just want it to come up quickly and remain until the home link is clicked in the menu.
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<?php
if(basename($_SERVER['SCRIPT_FILENAME']) == 'index.php'){
?>
<nav>
<ul class="nav navbar-nav">
<li>Home </li>
<li>Services </li>
<li>Search For Homes </li>
<li>Recent Transacctions</li>
</ul>
</nav>
<?php
}else{
?>
<nav>
<ul class="nav navbar-nav">
<li>Home </li>
<li>Services </li>
<li>Search For Homes </li>
<li>Recent Transacctions</li>
</ul>
</nav>
<?php
}
?>
</div>
For else condition when you are on contact-us page.
Change URL as below:
<nav>
<ul class="nav navbar-nav">
<li>Home </li>
<li>Services </li>
<li>Search For Homes </li>
<li>Recent Transacctions</li>
</ul>
</nav>
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');
}
Thank you for reading... I have a 2 layer of navigation. which sort the content by good, bad and comment count. if user clicks on good it will bring another nav which is sort the content by time. such as today, last one week, last one month.
I can add class='active' using $_SERVER['PHP_SELF'] but twitter bootsrap dropdown menu does not have simple class='active' it is complicated. I am not able to come up with a solution. should I use for? foreach? I don't know. please see the html command in the code. this is my code.
<ul class="nav nav-tabs">
<li <?php if (basename($_SERVER['PHP_SELF']) == '/?Top'): ?> class="active"<?php endif; ?>>Top Content</li>
<li <?php if (basename($_SERVER['PHP_SELF']) == '/?Bad'): ?> class="active"<?php endif; ?>>Bad Content</li>
<li <?php if (basename($_SERVER['PHP_SELF']) == '/?Comments'): ?> class="active"<?php endif; ?>>Most Commented</li>
<!-- It's confusing me when It comes here... because as you see active option is not li tag. it's bunch of html code and also any url bellow can ben any of those up -->
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown" href="#"><i class="icon-time"></i> Today <b class="caret"></b></a>
<ul class="dropdown-menu">
<li><a href="URL=week" />Last 1 Week</a></li>
<li><a href="URL=month" />Last 1 Month </a></li>
<li><a href="URL=all" />All</a></li>
</ul>
</li>
</ul>
html commend: It's confusing me when It comes here... because as you see active li is not li it's bunch of html code and also also any url below can be any of those up
You can use the "active" class for the outter li (the one has class "dropdown") and another "active" class in your child dropdown.
This is an example
<ul class="nav nav-tabs">
<li class="">Home</li>
<li>Help</li>
<li class="dropdown active">
Dropdown <b class="caret"></b>
<ul class="dropdown-menu">
<li>Action</li>
<li class="active">Another action</li>
<li>Something else here</li>
<li class="divider"></li>
<li>Separated link</li>
</ul>
</li>
</ul>
A live demo in JSBin