WordPress - Adding .active class to active menu item's sub pages? - php

There is a problem in wp_nav_menu below:
Nav.php:
$arg = array(
'container' => false,
'menu_class' => 'nav navbar-nav navbar-right'
);
wp_nav_menu($arg);
functions.php:
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;
}
Output:
<ul id="menu-quinn-menu" class="nav navbar-nav navbar-right">
<li id="menu-item-15" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-15">Home</li>
<li id="menu-item-14" class="menu-item menu-item-type-post_type menu-item-object-page current-menu-item page_item page-item-6 current_page_item current_page_parent menu-item-14 active ">Work</li>
<li id="menu-item-13" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-13">Shop</li>
<li id="menu-item-12" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-12">Contact</li>
</ul>
You can see the class active in the Work's li, but when I go to the sub pages of Work (parent), the output:
<ul id="menu-quinn-menu" class="nav navbar-nav navbar-right">
<li id="menu-item-15" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-15">Home</li>
<li id="menu-item-14" class="menu-item menu-item-type-post_type menu-item-object-page current_page_parent menu-item-14">Work</li>
<li id="menu-item-13" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-13">Shop</li>
<li id="menu-item-12" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-12">Contact</li>
</ul>
You don't see the class active in the Work's li anymore.
Any ideas how I can have active in the Work's li's sub pages?

Try changing the block you pasted from your functions.php file from:
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;
}
to:
add_filter('nav_menu_css_class' , 'special_nav_class' , 10 , 2);
function special_nav_class($classes, $item){
if( in_array('current_page_parent', $classes) ){
$classes[] = 'active ';
}
return $classes;
}
Alternatively, you could also use CSS and select current_page_parent since Wordpress automatically adds that class to the page's parent as well.

Related

I am trying to create a three tier navigation walker on wordpress, want the submenus to be side by side

This is what I am trying to achieve with a custom walker:
I am trying to get the following layout:
<nav id="nav-primary" class="mobile-menu">
<div id="menu-test-menu" class="menu genesis-nav-menu menu-primary js-superfish">
<div id="menu-item-25937"
class="menu-item menu-item-type-post_type menu-item-object-page current-menu-ancestor current-menu-parent current_page_parent current_page_ancestor menu-item-has-children menu-item-25937"> //this is the first tier
Products
</div>
<div class="sub-menu"> // this is the second tier
<div id="menu-item-25932"
class="menu-item menu-item-type-post_type menu-item-object-page menu-item-25932">phone system overview</div>
<div id="menu-item-25931"
class="menu-item menu-item-type-post_type menu-item-object-page menu-item-25931">phone system features</div>
<div id="menu-item-26230"
class="menu-item menu-item-type-post_type menu-item-object-page menu-item-home current-menu-item page_item page-item-25 current_page_item current-menu-ancestor current-menu-parent current_page_parent current_page_ancestor menu-item-26230">
dedivvery options
</div>
<div class="sub-menu"> // this is the third tier
<div id="menu-item-26234"
class="menu-item menu-item-type-post_type menu-item-object-page menu-item-home current-menu-item page_item page-item-25 current_page_item menu-item-26234">
CLOUD PHONE SYSTEM
</div>
<div id="menu-item-26235"
class="menu-item menu-item-type-post_type menu-item-object-page menu-item-home current-menu-item page_item page-item-25 current_page_item menu-item-26235">
MANAGED SIP TRUNKING
</div>
</div>
</div>
</nav>
PHP
function register_additional_menu() {
register_nav_menu( 'third-menu' ,__( 'Third Navigation Menu' ));
}
add_action( 'init', 'register_additional_menu' );
add_action( 'genesis_header', 'add_third_nav_genesis' );
function add_third_nav_genesis() {
wp_nav_menu( array(
'theme_location' => 'third-menu',
'menu' => '',
'container' => 'nav',
'container_id' => 'nav-primary',
'container_class' => 'mobile-menu',
'items_wrap' => '<div id="menu-test-menu" class="menu genesis-nav-menu menu-primary js-superfish">%3$s</div>',
'item_spacing' => 'discard',
'depth' => 3,
'walker' => new My_Custom_Nav_Walker
) );
}
I am basically trying to have a three tier menu like
menu item >> sub menu item >> sub sub menu item
sub menu item sub sub menu item
sub menu item sub sub menu item
And I need to have the third sub menu be aligned side by side with the first sub menu
Any help with this would be greatly appreciated.

Sub menus have disappeared

My web site has suddenly come down with some problems, with no known changes. Most prominently, no sub menus appear. The html is there, everything appears to be there in wordpress in the admin panel to be set up right. But no sub menus appear on hover. Site is btwimages.com.
<!-- ################ TOP MENU SECTION ################ -->
<!-- **Top-Menu** -->
<div id="top-menu">
<div class="container">
<ul id="menu-main-menu" class="menu"><li id="menu-item-2351" class="home menu-item menu-item-type-custom menu-item-object-custom current-menu-item current_page_item menu-item-home"><span class="hoverL"> <span class="hoverR"> </span> </span>Home</li>
<li id="menu-item-2352" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-has-children"><span class="hoverL"> <span class="hoverR"> </span> </span>About Us
<ul class="sub-menu">
<li id="menu-item-2316" class="menu-item menu-item-type-post_type menu-item-object-page">About BTW images</li>
<li id="menu-item-2318" class="menu-item menu-item-type-post_type menu-item-object-page">Testimonials</li>
<li id="menu-item-2317" class="menu-item menu-item-type-post_type menu-item-object-page">Our Team</li>
</ul>
</li>
<li id="menu-item-2353" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-has-children"><span class="hoverL"> <span class="hoverR"> </span> </span>Services
<ul class="sub-menu">
<li id="menu-item-2354" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-has-children">Real Estate
<ul class="sub-menu">
<li id="menu-item-2341" class="menu-item menu-item-type-post_type menu-item-object-page">Photography</li>
<li id="menu-item-2340" class="menu-item menu-item-type-post_type menu-item-object-page">Floor Plans</li>
<li id="menu-item-2343" class="menu-item menu-item-type-post_type menu-item-object-page">Virtual Tours</li>
<li id="menu-item-2342" class="menu-item menu-item-type-post_type menu-item-object-page">Video Tours</li>
<li id="menu-item-2339" class="menu-item menu-item-type-post_type menu-item-object-page">Brochures</li>
<li id="menu-item-3454" class="menu-item menu-item-type-post_type menu-item-object-page">TrueVision</li>
</ul>
</li>
<li id="menu-item-2888" class="menu-item menu-item-type-post_type menu-item-object-page">3D Immersion</li>
<li id="menu-item-3098" class="menu-item menu-item-type-post_type menu-item-object-page">Virtual Staging</li>
<li id="menu-item-2344" class="menu-item menu-item-type-post_type menu-item-object-page">Video</li>
<li id="menu-item-2336" class="menu-item menu-item-type-post_type menu-item-object-page">General Photography</li>
<li id="menu-item-2337" class="menu-item menu-item-type-post_type menu-item-object-page">Marketing</li>
</ul>
</li>
<li id="menu-item-2329" class="menu-item menu-item-type-post_type menu-item-object-page"><span class="hoverL"> <span class="hoverR"> </span> </span>Portfolio</li>
<li id="menu-item-2355" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-has-children"><span class="hoverL"> <span class="hoverR"> </span> </span>Pricing
<ul class="sub-menu">
<li id="menu-item-2333" class="menu-item menu-item-type-post_type menu-item-object-page">Real Estate Photography</li>
<li id="menu-item-2912" class="menu-item menu-item-type-post_type menu-item-object-page">3D Immersion Tour</li>
<li id="menu-item-2331" class="menu-item menu-item-type-post_type menu-item-object-page">Floor Plans</li>
<li id="menu-item-2332" class="menu-item menu-item-type-post_type menu-item-object-page">Brochures</li>
<li id="menu-item-3099" class="menu-item menu-item-type-post_type menu-item-object-page">Virtual Staging</li>
<li id="menu-item-3455" class="menu-item menu-item-type-post_type menu-item-object-page">TrueVision</li>
</ul>
</li>
<li id="menu-item-2356" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-has-children"><span class="hoverL"> <span class="hoverR"> </span> </span>News
<ul class="sub-menu">
<li id="menu-item-2328" class="menu-item menu-item-type-post_type menu-item-object-page">Latest News</li>
<li id="menu-item-2325" class="menu-item menu-item-type-post_type menu-item-object-page">BTWeen the lines</li>
<li id="menu-item-2327" class="menu-item menu-item-type-post_type menu-item-object-page">Latest Home Tours</li>
</ul>
</li>
<li id="menu-item-2357" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-has-children"><span class="hoverL"> <span class="hoverR"> </span> </span>Support
<ul class="sub-menu">
<li id="menu-item-2482" class="menu-item menu-item-type-post_type menu-item-object-page">FAQ</li>
<li id="menu-item-2910" class="menu-item menu-item-type-post_type menu-item-object-page">3D Immersion FAQs</li>
<li id="menu-item-2347" class="menu-item menu-item-type-post_type menu-item-object-page">Help Videos</li>
<li id="menu-item-2349" class="menu-item menu-item-type-post_type menu-item-object-page">Preparation Checklist</li>
<li id="menu-item-2348" class="menu-item menu-item-type-post_type menu-item-object-page">Media Terms of Use</li>
<li id="menu-item-2350" class="menu-item menu-item-type-post_type menu-item-object-page">Service Policies</li>
</ul>
</li>
<li id="menu-item-2358" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-has-children"><span class="hoverL"> <span class="hoverR"> </span> </span>Contact Us
<ul class="sub-menu">
<li id="menu-item-2320" class="menu-item menu-item-type-post_type menu-item-object-page">Learn More</li>
<li id="menu-item-2321" class="menu-item menu-item-type-post_type menu-item-object-page">Request Service</li>
</ul>
</li>
</ul> </div>
</div><!-- **Top-Menu - End** -->
<!-- ################ TOP MENU SECTION END ################ -->
If you can't fixed it through admin panel, you can add this code to your header or footer:
jQuery('.menu-item-has-children').hover(function() {
jQuery('.menu-item-has-children .sub-menu').css('display', 'none');
jQuery(this).find('.sub-menu').css('display', 'block');
});

Wordpress expand sub-menu when clicking on parent

I am making a wordpress theme using _s as a basis. I am trying to style the menu. So far I don't have any particular CSS on the menu. I have looked at the (responsive) menu for the Twenty Sixteen theme, and I want to have the same the of menu, but only the "responsive" menu for smaller screens. On my site, the menu does not have to be responsive though. The menu (on smaller screens) looks like this, which is what I want:
Twenty Sixteen theme demo: https://twentysixteendemo.wordpress.com/ (You have to resize to a small screen to see the menu I am talking about)
I want that type of menu largely because it has the ability for a "expand/drop-down button" that can show sub-content in the menu.
So my question is: how can I style my menu so that it looks like the Twenty Sixteen theme's (mobile/tablet) menu including the button that expands the menu sub-categories?
I have searched the web for days without finding info on this. It seems every article that merely address the issue are just copy-paste of other badly explained articles.
This is the rendered HTML I have for my menu right now:
<ul id="primary-menu" class="main-menu"><li class="has-children menu-item menu-item-type-custom menu-item-object-custom menu-item-has-children menu-item-1706">Group 1
<ul class="sub-menu">
<li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-has-children menu-item-1707">Sub Group 1
<ul class="sub-menu">
<li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-1708">Image</li>
<li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-1709">Image</li>
<li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-1710">Image</li>
</ul>
</li>
<li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-has-children menu-item-1711">Sub Group 2
<ul class="sub-menu">
<li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-has-children menu-item-1712">Sub Group Level 3
<ul class="sub-menu">
<li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-1713">Image</li>
<li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-1714">Image</li>
</ul>
</li>
<li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-1715">Image</li>
</ul>
</li>
<li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-1730">Image</li>
<li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-1731">Image</li>
</ul>
</li>
<li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-has-children menu-item-1718">Group 2
<ul class="sub-menu">
<li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-1719">Image</li>
<li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-1720">Image</li>
</ul>
</li>
<li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-has-children menu-item-1721">Group 3
<ul class="sub-menu">
<li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-1722">Image</li>
<li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-1723">Image</li>
</ul>
</li>
<li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-has-children menu-item-1724">Group 4
<ul class="sub-menu">
<li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-has-children menu-item-1725">Sub Group 3
<ul class="sub-menu">
<li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-1726">Image</li>
<li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-1727">Image</li>
</ul>
</li>
<li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-1728">Image</li>
<li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-1729">Image</li>
</ul>
</li>
</ul>
This is the menu in my header.php
<div class="menu-container">
<nav id="site-navigation" class="main-navigation" role="navigation">
<?php wp_nav_menu(array(
'theme_location' => 'primary',
'sort_column' => 'menu_order',
'menu' => 'Categories',
'container_class' => 'main-menu',
'menu_id' => 'primary-menu',
'menu_class' => 'main-menu'
) ); ?>
</nav><!-- #site-navigation -->
</div><!-- #menu-container -->
What my menu looks like now:
You can achieve this with javascript (jquery) or only with css.
Css example: https://codyhouse.co/gem/css-multi-level-accordion-menu/
jquery example: https://perishablepress.com/jquery-accordion-menu-tutorial/
hope this helps

WordPress: wp_nav_menu sometimes is messed up

I use the wordpress funcion wp_nav_menu() to display a hierarchical menu I created in my WP menu. Here's the snippet:
<?php wp_nav_menu('show_home=1&menu_class=page-navi&sort_column=menu_order'); ?>
Here's its html output:
<div class="menu-default-container"><ul class="page-navi" id="menu-default"><li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-127" id="menu-item-127">Home Page</li>
<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children menu-item-128" id="menu-item-128">Il film
<ul class="sub-menu">
<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-129" id="menu-item-129">Di che cosa parla?</li>
<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-133" id="menu-item-133">Una storia di occasioni</li>
<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-130" id="menu-item-130">Il Regista</li>
<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-131" id="menu-item-131">Il “maestro”</li>
</ul>
</li>
<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-139" id="menu-item-139">Incontri e discussioni</li>
<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-153" id="menu-item-153">News ed eventi</li>
<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children menu-item-141" id="menu-item-141">Film e lavoro in classe
<ul class="sub-menu">
<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-142" id="menu-item-142">Il titolo del film</li>
</ul>
</li>
<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-134" id="menu-item-134">Interviste</li>
<li class="menu-item menu-item-type-post_type menu-item-object-page current-menu-item page_item page-item-19 current_page_item menu-item-138" id="menu-item-138">Sponsor e collaborazioni</li>
<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-151" id="menu-item-151">Contatti</li>
<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-158" id="menu-item-158">Credits</li>
</ul></div>
The problem is that sometimes it's displayed properly:
.. and others isn't:
I checked the source code and it's THE SAME!! :o
Don't know what to think. These things scare me! :P
Here's the website if you want to try to reproduce this (if you're lucky.. in the nights of full moon)..
This is happening because of this style:
ul li {
display: inline;
padding: 2px;
}
Adding the following style to your css should fix your problem:
.menu-default-container ul li {
display: block !important;
}

how do I remove additional anchor tag in a bootstrapped WordPress dropdown menu?

Code used in functions.php to create WordPress [Dropdown enabled] Twitter Bootstrap
Menu: http://pastebin.com/HCxCcBmG
Code used in header.php: http://pastebin.com/7eTHTjij
Here's what gets generated for the dropdown menu code:
<ul class="dropdown-menu">
<li id="menu-item-29" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-29">
site design
</li>
<li id="menu-item-30" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-30">
app design
</li>
<li id="menu-item-31" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-31">
windows
</li>
<li id="menu-item-32" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-32">
mac
</li>
</ul>
How do I remove the additional anchor tag/link genereated in the first dropdown menu link? Like this:
<ul class="dropdown-menu">
<li id="menu-item-29" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-29">
site design
</li>
<li id="menu-item-30" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-30">
app design
</li>
<li id="menu-item-31" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-31">
windows
</li>
<li id="menu-item-32" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-32">
mac
</li>
</ul>

Categories