how can i prepare menu (arrays) for this code - wordpress

I am learning wordpress together with bootstrap and i don't kno how to prepare menu for following code :
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav nav-dropdown nav-right" data-app-modern-menu="true">
<li class="nav-item"><a class="nav-link link text-black display-4" href="">Item 1</a></li> <li class="nav-item"><a class="nav-link link text-black display-4" href="">Item 2</a></li>
<li class="nav-item"><a class="nav-link link text-black display-4" href="">Item 3</a></li>
<li class="nav-item"><a class="nav-link link text-black display-4" href="">Item 4</a></li>
</ul> </div>
for this code i am using
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<?php
wp_nav_menu(
array(
'menu' => 'primarym',
'container' => 'ul',
'container_class' => 'navbar-nav nav-dropdown nav-right',
'menu_class' => 'menu',
'menu_id' => 'primary-menu',
'echo' => true,
)
); ?>
</div>
which is not working in my templates, can anyone help me.

No need to create custom walker. Just use additional argument and set the filters for nav_menu_css_class and nav_menu_link_attributes.
Please check below which help you.
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<?php
wp_nav_menu(
array(
'menu' => 'primarym',
'container' => 'ul',
'container_class' => '',
'menu_class' => 'navbar-nav nav-dropdown nav-right',
'menu_id' => 'primary-menu',
'add_li_class' => 'nav-item',
'echo' => true,
)
); ?>
</div>
Notice the new 'add_li_class' argument.
And set the filters on functions.php
function add_additional_class_in_li($classes, $item, $args) {
if(isset($args->add_li_class)) {
$classes[] = $args->add_li_class;
}
return $classes;
}
add_filter('nav_menu_css_class', 'add_additional_class_in_li', 1, 3);
function add_additional_class_in_ancher($atts) {
$atts['class'] = "nav-link link text-black display-4";
return $atts;
}
add_filter( 'nav_menu_link_attributes', 'add_additional_class_in_ancher');

You're close with your function, just keep in mind that container is for the menu's container (in your case, the div tag) and menu is for the menu itself (in your case, the ul tag).
For those two elements, you'll have to set your arguments to:
wp_nav_menu(
array(
'menu' => 'primarym', // or the id of the menu (you could otherwise use theme_location)
'container' => 'div',
'container_id' => 'navbarSupportedContent',
'container_class' => 'collapse navbar-collapse',
'menu_class' => 'navbar-nav nav-dropdown nav-right',
'items_wrap' => '<ul class="%2$s" data-app-modern-menu="true">%3$s</ul>',
'walker' => new My_Walker(),
)
);
You'll notice that there's a new walker argument, without which you won't be able to display the menu element itself (li and a tags) the way you want.
A walker is a core class used to implement an HTML list of nav menu items and here's how you could use it for your example :
class My_Walker extends Walker_Nav_Menu {
function start_lvl( &$output, $depth, $args ) {
}
function start_el( &$output, $item, $depth, $args ) {
// $output : var returned at the end of the walker
// $item : information about the current item
$output .= sprintf(
'<li class="nav-item"><a class="nav-link link text-black display-4" href="%1$s">%2$s',
$item->url,
$item->title
);
}
function end_el( &$output, $item, $depth, $args ) {
// $output : var returned at the end of the walker
$output .= '</a></li>';
}
function end_lvl( &$output, $depth, $args ) {
}
}

Related

How can i set tow <ul> in wp_nav_menu in wordpress?

I have a problem to set two ul in wp_nav_menu.
i have set like following code in html. i want to set in wp_nav_menu.
my code is.
<div class="collapse navbar-collapse menu-header-menu-container" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li>Home</li>
<li>About Us</li>
<li>Athletes</li>
<li>Coach</li>
<li>Colleges</li>
<li>Contact Us</li>
</ul>
<ul class="user-action hidden-xs">
<li>Seach</li>
<li>Login</li>
<li>Register</li>
</ul>
</div>
So anyone can help me how to set two ul in wp_nav_menu.
First thing you need to do is register the 2 menus in the functions.php file.
function register_menus() {
register_nav_menus(
array(
'menu-1' => __('Primary Menu'),
'menu-2' => __('Secondary Menu')
)
);
}
add_action('init', 'register_menus');
Then in the WordPress dashboard under Appearance > Menus create the menus.
Be sure to set the theme locations to 'Primary Menu' and 'Secondary Menu'.
Place the following code where you want the menus to display (most likely header.php).
<?php wp_nav_menu(array('menu' => 'menu-1', 'container' => '<ul>', 'menu_class' => 'nav navbar-nav')); ?>
<?php wp_nav_menu(array('menu' => 'menu-2', 'container' => '<ul>', 'menu_class' => 'user-action hidden-xs')); ?>
If someone else has issues to fetch the right menu, you can try to exchange 'menu' to 'theme_location'.
<?php wp_nav_menu(array('theme_location' => 'menu-1', 'container' => '<ul>', 'menu_class' => 'marquee__inner')); ?>

Register Custom menu in wordpress

How to register this menu in wordpress dynamically ?
i'am having problem to dynamic menu in wordpress
<nav id="nav">
<ul>
<li>Division
<ul class="dropotron dropotron-level-0 center" style="-moz-user-select: none; position: absolute; z-index: 1000; left: 184.767px; top: 29px; opacity: 1; display: none;">
<li ><a style="display: block;" href="real-estate.html">Real Estate</a></li>
<li><a style="display: block;" href="waterpump.html">Water Pump</a></li>
<li ><a style="display: block;" href="fmcg.html">FMCG</a></li>
<li ><a style="display: block;" href="infrastructure.html">Infrastructure Project</a></li>
</ul>
</li>
<li >About Us</li>
<li class="break">Career</li>
<li >Contact Us</li>
</ul>
</nav>
Thanks
i'm not sure i'm fully understand what you'r meant when you say 'register this menu in wordpress dynamically', but if you meant to register custom menu and display it so follow this:
put this code on the 'functions.php' file: (if you are not using a theme of your own then create a child theme if you didn't already)
function register_your_menu() {
register_nav_menu('your-menu-location',__( 'Your Menu Description' ));
}
add_action( 'init', 'register_your_menu' );
*register your new menu - it will display the new menu on the admin panel so set it as you like.
put this code on the place you want the menu to appear on the theme and customize the '$defaults' settings as you like (you can find more info here: http://codex.wordpress.org/Function_Reference/wp_nav_menu):
if ( has_nav_menu( 'your-menu-location' ) ) {
$defaults = array(
'theme_location' => 'your-menu-location',
'menu' => '',
'container' => 'div',
'container_class' => '',
'container_id' => '',
'menu_class' => 'menu',
'menu_id' => '',
'echo' => true,
'fallback_cb' => 'wp_page_menu',
'before' => '',
'after' => '',
'link_before' => '',
'link_after' => '',
'items_wrap' => '<ul id="%1$s" class="%2$s">%3$s</ul>',
'depth' => 0,
'walker' => ''
);
wp_nav_menu( $defaults );
}

wp_nav_menu() doesn't work poperly

I need to use custom items_wrap format but it seems like it doesn't work at all.
<?php wp_nav_menu( array(
'menu_class' => 'menu',
'menu' => 'mobile-menu',
'theme_location' => 'main-navigation',
'container' => false,
'items_wrap' => '<ul data-role="listview">%3$s</ul>' ) ); ?>
The OUTPUT
<div class="menu">
<ul>
<li class="page_item page-item-2">Sample Page</li>
</ul>
Problem is that in output <ul> doesn't have data-role assigned as it should have.
Anyone have an idea why this function doesn't get the parameter right?
You could try this code:
<ul class="menu">
<?php wp_nav_menu( array(
'menu' => 'mobile-menu',
'theme_location' => 'main-navigation',
'container' => false,
'items_wrap' => '%3$s' ) ); ?>
</ul>

Wordpress Custom Navigation Menus

Hi i have the following navigation menus from html css template. I want to change it in wordpress navigation. But CSS doesnot work there. How can i save the css without losing its features.
<!--Header-->
<header class="navbar navbar-fixed-top">
<div class="navbar-inner">
<div class="container">
<a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</a>
<a id="lgo" class="pull-left" href="index.html"><H1>Global BizVisions, LLC</h1></a>
<div class="nav-collapse collapse pull-right">
<ul class="nav">
<li class="active">Home</li>
<li>About Us</li>
<li>Services</li>
<li>Portfolio</li>
<li class="dropdown">
Pages <i class="icon-angle-down"></i>
<ul class="dropdown-menu">
<li>Career</li>
<li>Blog Single</li>
<li>FAQ</li>
<li>Pricing</li>
<li>404</li>
<li>Typography</li>
<li>Registration</li>
<li class="divider"></li>
<li>Privacy Policy</li>
<li>Terms of Use</li>
</ul>
</li>
<li>Blog</li>
<li>Contact</li>
<li class="login">
<a data-toggle="modal" href="#loginForm"><i class="icon-lock"></i></a>
</li>
</ul>
</div><!--/.nav-collapse -->
</div>
</div>
</header>
<!-- /header -->
I did is like this:
<!--Header-->
<header class="navbar navbar-fixed-top">
<div class="navbar-inner">
<div class="container">
<a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</a>
<h1>Global BizVisions, LLC</h1>
<div class="nav-collapse collapse pull-right">
<ul>
<?php wp_nav_menu( array( 'sort_column' => 'menu_order', 'container_class' => 'nav' ) ); ?>
<ul>
</div><!--/.nav-collapse -->
</div>
</div>
</header>
<!-- /header -->
and what are the things i need to change in functions.php or other.
Thanks in advance
you need create a walker in your functions.php look:
add_filter('nav_menu_css_class' , 'special_nav_class' , 10 , 2);
function special_nav_class($classes, $item){
if( in_array('current-menu-item', $classes) ){
$classes[] = 'active ';
}
return $classes;
}
class My_Custom_Nav_Walker extends Walker_Nav_Menu {
function start_lvl(&$output, $depth = 0, $args = array()) {
$output .= "\n<ul class=\"dropdown-menu\">\n";
}
function start_el(&$output, $item, $depth = 0, $args = array(), $id = 0) {
$item_html = '';
parent::start_el($item_html, $item, $depth, $args);
if ( $item->is_dropdown && $depth === 0 ) {
$item_html = str_replace( '<a', '<a class="dropdown-toggle" data-toggle="dropdown"', $item_html );
$item_html = str_replace( '</a>', ' <b class="caret"></b></a>', $item_html );
}
$output .= $item_html;
}
function display_element($element, &$children_elements, $max_depth, $depth = 0, $args, &$output) {
if ( $element->current )
$element->classes[] = 'active';
$element->is_dropdown = !empty( $children_elements[$element->ID] );
if ( $element->is_dropdown ) {
if ( $depth === 0 ) {
$element->classes[] = 'dropdown';
} elseif ( $depth === 1 ) {
// Extra level of dropdown menu,
// as seen in http://twitter.github.com/bootstrap/components.html#dropdowns
$element->classes[] = 'dropdown-submenu';
}
}
parent::display_element($element, $children_elements, $max_depth, $depth, $args, $output);
}
}
in your header.php (if your menu location is header) at the div .navbar-collapse:
erase the ul tag.
the ul is created in the wp_nav_menu:
<?php wp_nav_menu(array(
'theme_location' => 'nav_social',
'container' => false,
'walker' => new My_Custom_Nav_Walker(),
'items_wrap' => '<ul id="your_nav" class="navbar-right"></li>%3$s</ul>'
)
); ?>
remember you need create the menu on the functions.php
function register_my_menus(){
register_nav_menus(array(
'your_nav' => __('your_nav', 'siddharta naranjo')));}add_action('init','register_my_menus');
well
1- Go to wp-admin then
Apperance=>menu
2- create a menu
$nav_menu= array(
'theme_location' => '',
'menu' => 'here menu name',
'container' => 'div',
'container_class' => '',
'container_id' => '',
'menu_class' => 'sticky',
'menu_id' => '',
'echo' => true,
'fallback_cb' => 'wp_page_menu',
'before' => '',
'after' => '',
'link_before' => '',
'link_after' => '',
'items_wrap' => '<ul id="%1$s">%3$s</ul>',
'depth' => 0,
'walker' => ''
);
wp_nav_menu( $nav_menu);
I think it will work out fine for you

wp_nav_menu not outputting as expected from configuration

I've got a nav function in functions.php:
function html5blank_nav()
{
wp_nav_menu(
array(
'theme_location' => 'header-menu',
'menu' => '',
'container' => false,
'menu_class' => 'nav',
'menu_id' => '',
'echo' => true,
'fallback_cb' => 'wp_page_menu',
'before' => '',
'after' => '',
'link_before' => '',
'link_after' => '',
'items_wrap' => '<ul class="%2$s">%3$s</ul>',
'depth' => 0,
'walker' => ''
)
);
}
And it's called in header.php with:
<?php html5blank_nav(); ?>
And my output is:
<div class="nav">
<ul>
<li class="page_item page-item-2">Sample Page</li>
<li class="page_item page-item-4">Test page</li>
</ul>
</div>
But What I was trying to achieve was this structure:
<ul class="nav">
<li>Sample Page</li>
<li>Test page</li>
</ul>
I thought that setting container=false would get rid of the containing divs, and that setting menu_class='nav' would add the nav class to the ul as it states here: http://codex.wordpress.org/Function_Reference/wp_nav_menu
Why isn't it recognising my settings?
from the Codex:
In order to remove navigation container, theme location specified in functions.php and used among arguments in function wp_nav_menu ( eg. 'theme_location' => 'primary-menu' ) must have a menu assigned to it in administration! Otherwise argument 'container' => 'false' is ignored

Resources