Mega menu WordPress walker - wordpress

Here is my menu structure, how I build a mega menu walker, how I do this ?
<nav>
<ul class="main-nav navbar-right">
<li class="active dropdown">Home
<div class="sub-menu">
<ul class="home">
<li>Home</li>
<li><a class="last" href="index-2.html">Home2</a></li>
</ul>
</div>
</li>
<li class="dropdown">Pages
<div class="mega-menu pages">
<span class="mega-menu-list">
Blog1
Blog2
Blog3
</span>
<span class="mega-menu-list">
Single Blog 1
Single Blog 2
Single Blog 3
Single Blog 4
</span>
<span class="mega-menu-list">
404 Page
Contact Us
</span>
</div>
</li>
<li class="dropdown">Sport</li>
<li>Travel</li>
<li>Lifestyle</li>
<li>Tech</li>
<li><a href=">Contact Us</a></li>
</ul>
</nav>

You can put Your menu structure HTML in this code.
/* Drop Down Menu */
class ik_walker extends Walker_Nav_Menu{
function start_lvl(&$output, $depth) {
if($depth == 0){
$output .= 'HTML Start Here for Parent Menu
';
}
if($depth == 1){
$output .= '<ul class="sub-child"> Start Here for child Menu';
}
}
function end_lvl(&$output, $depth) {
if($depth == 0){
$output .= 'End For Parent Menu
';
}
if($depth == 1){
$output .= '</ul> End for child';
}
}
}
and Use this function in Menu Like this.
if ( has_nav_menu( 'primary' ) ) :
wp_nav_menu( array(
'theme_location' => 'primary',
'menu_class' => 'horizontal_links navigations',
'container' => '',
'depth' => 3,
'walker' => new ik_walker()
) );
endif;

Related

how can i prepare menu (arrays) for this code

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 ) {
}
}

Wordpress menu issue - second menu overriding first

I'm converting an html site to wordpress and am having an issue with the menus. I have created the separate menu locations with the php code in the templates
<div id="NavItems">
<?php
$args = array(
'theme-location' => 'primary',
);
wp_nav_menu($args);
?>
</div>
<div class="footerNav">
<h3>Services</h3>
<?php
$args = array(
'theme-location' => 'footerServ',
);
wp_nav_menu($args);
?>
</div>
And the functions.php has this code
function register_my_menus() {
register_nav_menus(array(
'primary' => __('Primary Menu'),
'footerServ' => __('Footer Services'),
'footerWork' => __('Footer Our Work'),
'footerLegal' => __('Footer Legal'),
'footerInc' => __('Footer Inc'),
));
}
add_action( 'init', 'register_my_menus' );
main menu config looks like this -
Main Menu Wordpress Config
footer menu config looks like this -
FooterMenu Wordpress Config
The resulting html looks like this for the main menu
<nav role="navigation" id="MainNav" class="group">
...
<div id="NavItems">
<div class="menu-footer-services-container">
<ul id="menu-footer-services" class="menu">
<li id="menu-item-95" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-95">
Mold Manufacturing
</li>
<li id="menu-item-96" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-96">
Injection Molding
</li>
<li id="menu-item-94" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-94">
Thermoforming
</li>
<li id="menu-item-93" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-93">
Plastic Fabrication
</li>
<li id="menu-item-91" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-91">
Custom Design
</li>
<li id="menu-item-92" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-92">
Assembly
</li>
</ul>
</div>
</div>
...
The footer menu has the same class and ul.
I have done everything I can think of, and am almost certain the configuration is correct inside of wordpress, and haven't found any issues with my code.
In summary, My main menu is being overwritten by the footer menu, despite having different locations set, and names. The wordpress dashboard has the correct configuration for what I want. However the result shows my main menu having the footer menu class and instead of 'home'... it shows 'mold manufacuring'....
I was following this video (in comments) to set up my navigation, and followed his actions exactly.
It's almost as if wordpress is only allowing me to show 1 menu, despite allowing me to configure several.
Thank you in advance for any help.
<div id="NavItems">
<?php
$args = array(
'theme-location' => 'primary',
);
wp_nav_menu($args);
?>
</div>
Looks like the issues lies in the 'theme-location', seems to work if changed to 'theme_location' instead. However I will stay with my word-around because it would take more time to change it back for equal functionality.
If anyone is interested, the workaround looks like this
<div id="NavItems">
<ul>
<?php
$servMenu = wp_get_nav_menu_items('(wordpress number for your menu');
foreach ($servMenu as $item) {
$pages = get_pages();
$class = "";
foreach ($pages as $page) {
if ($page->post_title == $item->title) {
if (is_page($page->ID)) {
$class = "current-menu-item";
}
break;
}
} ?>
<li class="<?php echo $class; ?>">
<a href="<?php echo $item->url; ?>">
<?php echo $item->title; ?>
</a>
</li>
<?php }
?>
</ul>
</div>
Use menu_class in args like this it will take as ul class of menu.
<div id="NavItems">
<?php
$args = array(
'theme-location' => 'primary',
'menu_class' => 'primary-class',
);
wp_nav_menu($args);
?>
</div>
<div class="footerNav">
<h3>Services</h3>
<?php
$args = array(
'theme-location' => 'footerServ',
'menu_class' => 'footer-class',
);
wp_nav_menu($args);
?>
</div>

Wordpress custom post loop for slider

How can i make looping for this slider :
My Slider looks like : http://www.awesomescreenshot.com/image/969867/a3a51b333881ca762739151dae8b16f9
First li consist of top image and second one comes with below one. each single image must be a single post
Html Code :
<div class="row cycle-slideshow" data-cycle-fx=carousel data-cycle-timeout=0 data-cycle-carousel-visible=3 data-cycle-carousel-fluid=true data-cycle-slides="li" data-cycle-next="#next" data-cycle-prev="#prev" data-cycle-pager="#pager">
<ul>
<li class="span2">
<a href="#journeymodel">
<img src="http://lorempixel.com/200/200/">
<h3>Barobudur Temple, java</h3>
<div></div>
</a>
<a href="#journeymodel">
<img src="http://lorempixel.com/200/200/">
<h3>Barobudur Temple, java</h3>
<div></div>
</a>
</li>
<li class="span2">
<a href="#journeymodel">
<img src="http://lorempixel.com/200/200/">
<h3>Uluvatu Temple, Bali</h3>
<div></div>
</a>
<a href="#journeymodel">
<img src="http://lorempixel.com/200/200/">
<h3>Uluvatu Temple, Bali</h3>
<div></div>
</a>
</li>
Php code :
<?php
$loop = new WP_Query(array('post_type'=>'realisation','sort_column' => 'post_date','posts_per_page'=> -1);//'order' => 'DESC');
if ( $loop->have_posts() )
{
while ( $loop->have_posts() )
{
$loop->the_post();
$cat=get_post_meta($post->ID,'category',true);
if ($cat=='privae') // post catagory
{
//li section should comes here by based on list of post added
Can someone guide me how can i update the looping for this section .
You can do something like this:
$arr = array("post_type" => "post_type", 'posts_per_page' => -1, 'orderby' => 'post_title', 'order' => 'ASC');
$posts= get_posts($arr);
echo '<ul>';
foreach($posts as $post) {
$img= get_post_meta($post->ID, 'meta_field', true);
echo "<li><img src='" . $img . "' /></li>";
}
echo '</ul>';

Show WordPress Primary Menu in Zurb Foundation header [_s theme ]

I am creating a theme based on _s & Zurb's Foundation.
I have pretty much got everything set up and ready to start into the CSS, but I am having issues getting the 'primary menu' to show on the top bar.
I want to leave the 'left nav button' where it is, for a highlighted link (possibly contact us or similar), but I want to replace the current content in ul class="right" for the WordPress Primary Menu.
Here is what I have in the current header id="masthead"
<header id="masthead" class="site-header" role="banner">
<div class="row">
<div class="site-branding">
<h1 class="site-title">
<?php bloginfo( 'name' ); ?></h1>
<h2 class="site-description"><?php bloginfo( 'description' ); ?></h2>
</div>
<div class="contain-to-grid sticky">
<nav id="site-navigation" class="top-bar" data-topbar data-options="sticky_on: large" role="navigation">
<ul class="title-area">
<li class="name">
<h4><a href="<?php echo esc_url( home_url( '/' ) ); ?>" rel="home">
<?php bloginfo( 'name' ); ?></a></h4>
</li>
<li class="toggle-topbar menu-icon">Menu</li>
</ul>
<section class="top-bar-section">
<!-- Right Nav Section -->
<ul class="right">
<li class="active">Right Button Active</li>
<li class="has-dropdown">
Right Button Dropdown
<ul class="dropdown">
<li>First link in dropdown</li>
</ul>
</li>
</ul>
<!-- Left Nav Section -->
<ul class="left">
<li>Left Nav Button</li>
</ul>
</section>
</nav><!-- #site-navigation -->
</div>
</div> <!-- row -->
</header><!-- #masthead -->
You need to use wp_nav_menu().
http://codex.wordpress.org/Function_Reference/wp_nav_menu
You will also need to use a walker which customises the output of wp_nav_menu() to match that of Foundation's top bar.
You can add this to your funtions.php
class guts_top_bar_walker extends Walker_Nav_Menu {
function display_element( $element, &$children_elements, $max_depth, $depth = 0, $args, &$output ) {
$element->has_children = !empty($children_elements[$element->ID]);
$element->classes[] = ( $element->current || $element->current_item_ancestor ) ? 'active' : '';
$element->classes[] = ( $element->has_children ) ? 'has-dropdown' : '';
parent::display_element( $element, $children_elements, $max_depth, $depth, $args, $output );
}
function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
$item_html = '';
parent::start_el( $item_html, $item, $depth, $args, $id );
$output .= ($depth == 0) ? '<li class="divider"></li>' : '';
$classes = empty($item->classes) ? array() : (array) $item->classes;
if ( in_array( 'section', $classes ) ) {
$output .= '<li class="divider"></li>';
$item_html = preg_replace('/<a[^>]*>(.*)<\/a>/iU', '<label>$1</label>', $item_html);
}
$output .= $item_html;
}
function start_lvl( &$output, $depth = 0, $args = array() ) {
$output .= "\n<ul class=\"sub-menu dropdown\">\n";
}
}
You specify the walker within wp_nav_menu(). Replace the section area of the top bar in your header with the following;
<section class="top-bar-section">
<?php wp_nav_menu(array('container' => false, 'menu_class' => 'left', 'theme_location' => 'primary-left', 'fallback_cb' => false, 'walker' => new guts_top_bar_walker() )); ?>
<?php wp_nav_menu(array('container' => false, 'menu_class' => 'right', 'theme_location' => 'primary', 'walker' => new guts_top_bar_walker() )); ?>
</section>
Even if you want a highlighted link to remain in the left section, I'd recommend setting up another menu so that you can make future changes directly through the wordpress dash, instead of requiring edits to your theme.
You can register a new menu area like this in functions.php:
register_nav_menu( 'primary-left', __( 'Left Navigation Menu', 'guts' ) );
more info here: http://codex.wordpress.org/Function_Reference/register_nav_menus

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

Resources