Remove sub-menu span in wp_nav_menu - wordpress

I have a piece of HTML menu which I want to display in Wordpress with wp_nav_menu.
The HTML
<nav id="navigation" class="ddsmoothmenu">
<ul id="main-menu">
<li><span>Home</span>
<ul>
<li>Home Alternate 2</li>
<li>Home Alternate 3</li>
<li>Home Alternate 4</li>
<li>Home Alternate 5</li>
<li>Home Alternate 6</li>
</ul>
</li>
</ul>
I am adding the following in my Wordpress header.php file to display the nav menu
$defaults = array(
'theme_location' => 'primary',
'menu' => 'Primary Menu',
'container' => '',
'container_class' => '',
'container_id' => '',
'menu_class' => 'menu',
'menu_id' => '',
'echo' => true,
'fallback_cb' => 'wp_page_menu',
'before' => '',
'after' => '',
'link_before' => '<span>',
'link_after' => '</span>',
'items_wrap' => '<ul id="%1$s" class="%2$s">%3$s</ul>',
'depth' => 0,
'walker' => ''
);
wp_nav_menu($defaults);
However the output is adding the tags also in the second level of the menu items as
<nav id="navigation" class="ddsmoothmenu">
<ul id="main-menu">
<li><span>Home</span>
<ul>
<li><span>Home Alternate 2</span></li>
<li><span>Home Alternate 3</span></li>
<li><span>Home Alternate 4</span></li>
<li><span>Home Alternate 5</span></li>
<li><span>Home Alternate 6</span></li>
</ul>
</li>
</ul>
Is there a way to add the span only for the top level menu item or remove the spans from the second level items?

add_filter( 'wp_nav_menu_objects', function( $items ) {
foreach ( $items as $item ) {
if (!$item->menu_item_parent) {
$item->title = '<span>' . $item->title . '</span>';
}
}
return $items;
});

Ok, While walker seems to be one good way of getting this done, it seems to be pretty elaborate with many lines of codes. I just found out that the easiest way to remove the span for the sub-menu items is with jQuery. Wordpress automatically adds the sub-menu class to the child ul and hence what worked for me was
$('ul.sub-menu li a span').contents().unwrap();
Hope this helps.

Using CSS you can hide the sub-menu of wp_nav_menu
.sub-menu { display:none; }

Related

Navigation Items In Page But Not Displaying WordPress

During the process of working on my new WordPress site using Humescores as the theme, I noticed that my primary navigation menu stopped displaying. Upon inspection though, one can see that the navigation and li items are indeed in the page. But I cannot get them to render. I've set the background color of the navigation to red with a yellow border so you can see where it is. And I've attempted to set the font color to white but you can see none of the links are displaying. Very wierd behavior. What gives?
From functions.php
register_nav_menus( array(
'primary' => esc_html__( 'Header', 'humescores' ),
) );
From header.php
<nav id="site-navigation" class="main-navigation" style="border: 2px solid yellow; background-color:red; color:#fff;">
<button class="menu-toggle" aria-controls="primary-menu" aria-expanded="false"><?php esc_html_e( 'Primary Menu', 'humescores' ); ?></button>
<?php
wp_nav_menu( array(
'theme_location' => 'primary',
'menu_id' => 'primary-menu',
) );
?>
</nav><!-- #site-navigation -->
ChromeDevToolsScreenShot
Try to add z-index:99999; inside the css for the navigation.

Mobile: hide menu after clicking menu link

header.php
<div class="mobile-menu">
<i class="fas fa-bars"></i>
</div>
<div class="site-nav">
<ul>
<?php
// navigation menu
wp_nav_menu(array(
'theme_location' => 'primary',
'menu_class' => 'primary-menu',
'container' => false,
'items_wrap' => '%3$s'
));
?>
</ul>
</div>
consider this as
.site-nav ul li a
JS
jQuery(document).ready(function(){
jQuery(window).load(function() { //execute after load
//responsive menu
jQuery('.mobile').on('click',function() {
jQuery('.site-nav').slideToggle('slow');
});
//fix menu when changing to mobile to desktop
var breakpoint = 767;
jQuery(window).resize(function(){
if (jQuery(document).width() >= breakpoint){
jQuery('.site-nav').show();
}else{
jQuery('.site-nav').hide();
}
});
});
});
how to hide menu page when I click other page link, every time i click the other page the menu wont hide/toggle
http://jsfiddle.net/jayllopy/vds2U/217/

catching elements when displaying others using css

I have this class for my menu: .site-header-menu
and I have this class for my content: .content
what should I do to catch .content when .site-header-menu is displayed?
I already tried this but it doesn't work:
.site-header-menu + .content{
opacity: 0;
}
Do you have some suggestion? This kind of things are possible using css?
I'm using Wordpress so this is my php struture in header.php. The idea is to hide the content of my page template when site-header-menu is open:
<?php if ( has_nav_menu( 'primary' ) || has_nav_menu( 'social' ) ) : ?>
<!--<button id="menu-toggle" class="menu-toggle"><?php _e( 'Menu', 'twentysixteen' ); ?></button>-->
<button id="menu-toggle" class="menu-toggle" data-title="<?php the_title(); ?>"><?php the_title(); ?></button>
<div id="site-header-menu" class="site-header-menu">
<?php if ( has_nav_menu( 'primary' ) ) : ?>
<nav id="site-navigation" class="main-navigation" role="navigation" aria-label="<?php esc_attr_e( 'Primary Menu', 'twentysixteen' ); ?>" data-title= ''>
<?php
wp_nav_menu( array(
'theme_location' => 'primary',
'menu_class' => 'primary-menu',
) );
?>
</nav><!-- .main-navigation -->
<?php endif; ?>
How is .site-header-menu being displayed? Is a class being added to display the menu or is its display property being directly manipulated by JavaScript? There is no indication how you can tell if the menu is displayed?
.site-header-menu + .content{
opacity: 0;
}
The above rule, because of the plus sign, will only apply if the element with class content is immediately following the element with class site-header-menu in the DOM. For example:
<div class="site-header-menu">
... menu stuff ...
</div>
<div class="content">
... content stuff ...
</div>
This might be the problem, but without actually seeing the output HTML it's hard to say.
If you are using PHP or JS to toggle the visibility of the menu you could potentially add/remove an additional class to .content that will hide/show it based on the menu being displayed or not.
/* CSS rule to hide the .content element */
.content.hideMe {
visibility: none;
}
// pseudo-script click event handler (NOT REAL SCRIPT!)
function menuButtonClick() {
if (menuCurrentlyDisplayed) {
// menu is already displayed so hide it and show content
hideMenu;
removeClass(".content", "hideMe");
} else {
// menu is not yet displayed so show it and hide content
showMenu;
addClass(".content", "hideMe");
}
}
If you could post your output HTML, or set up a fiddle for us to take a look, it would be helpful.

Wordpress loses padding on custom sidebar

Here's a strange problem!
I'm using a custom sidebar which replicates the normal default sidebar except that it adds a relevant google maps widget for that page showing location of that particular event.
the map widget is not needed on any other page so that's why an almost identical custom menu has been created but with the extra map widget.
on the first image you can see the default sidebar on the right has some additional classes (.widget) and also
all the widgets are contained in divs with ids for each div
so the padding is as correct
on the second image all of the are widgets lacking don't have divs anymore so no place to add a custom classes on custom.css
required padding is missing!
if the classes were there this wouldn't be a problem!
I think i did the following:
created a custom sidebar
added widgets
created a page and replaced default rhs sidebar at time of creation
have I done it wrong?
surely the wordpress core should be adding all classes and divs with their respective id's
=====================================================================
1st edit
I used the custom sidebar and configured widgets.
all elements of the jummah custom sidebar are widgets
here's the html...
<div class="sidebar s2">
<a class="sidebar-toggle" title="Expand Sidebar">
<div class="sidebar-content">
<div class="sidebar-top group">
Jummah, Darutawheed Oxford
<p>
<p>
<p>Barak Allaahu feekum</p>
<form class="searchform themeform" action="http://darutawheed.co.uk/" method="get">
Tags
<div class="tagcloud">
<div id="calendar_wrap">
Archives
<ul>
</div>
and the css that gets generated in Jummah sidebar
.col-3cm .s2 {
float: right;
margin-right: -260px;
position: relative;
right: -100%;
}
.s2 {
width: 260px;
z-index: 1;
}
.sidebar {
padding-bottom: 20px;
position: relative;
z-index: 2;
}
==================================
this is how it should look
html
<div class="sidebar s2">
<a class="sidebar-toggle" title="Expand Sidebar">
<div class="sidebar-content">
<div class="sidebar-top group">
<div id="search-2" class="widget widget_search">
<div id="tag_cloud-3" class="widget widget_tag_cloud">
<div id="calendar-2" class="widget widget_calendar">
<div id="archives-2" class="widget widget_archive">
</div>
css
.sidebar .widget {
padding: 30px 30px 20px;
}
.widget {
color: #777777;
font-size: 15px;
overflow: hidden;
}
================================
I did a little experiment
this pic shows page using the DEFAULT sidebar
this is the JUMMAH sidebar which totally breaks the design
I tried to add a custom sidebar widget class using edit sidebar BEFORE WIDGET
<div class="custom_sidebar_widget">
and AFTER WIDGET
</div>
and the copied the padding style like this in custom.css
.custom_sidebar_widget {
padding: 30px 30px 20px;
}
but that just padded out the widgets correctly but the styling (font colour and calendar style was all messed up)
The theme is not adding the necessary divs and classes widget widget_search, widget widget_tag, widget widget_calendar etc. so this breaking the design!!
I think I've covered everything
It's very difficult to view the images without coding. Anyway! I hope the problem was your coding. As I view your site, I saw the issue between your 2nd image and default code(live site). The thing is.
You didn't use a div for your content with calss .widget
See my example.
<div class="sidebar s2">
<a class="sidebar-toggle" title="Expand Sidebar"><!-- Something here I saw in live site --></a>
<div class="sidebar-content">
<div class="sidebar-top group"><!-- Something here I saw in live site --></div>
<!-- I don't know about codes. After this one use a div with .widget class -->
<div id="myTest" class="widget">
Jummah, Darutawheed Oxford
<p>Contents Here</p>
<p>Another P Tag contents</p>
<p>Then use form</p>
<form action="http://darutawheed.co.uk/" class="searchform themeform" method="get" data-dpmaxz-eid="1">
<div id="mysform">
<input type="text" value="To search type and hit enter" onfocus="if(this.value=='To search type and hit enter')this.value='';" onblur="if(this.value=='')this.value='To search type and hit enter';" name="s" class="search" data-dpmaxz-eid="2">
</div>
</form>
</div><!--end div#myTest-->
</div>
I just use that form static way. You just have to add that class with a div. See div #myTest
Thanks
See Theme Codes 1st Edit.
/* Register sidebars
/* ------------------------------------ */
if ( ! function_exists( 'alx_sidebars' ) ) {
function alx_sidebars() {
register_sidebar(array( 'name' => 'Primary','id' => 'primary','description' => "Normal full width sidebar", 'before_widget' => '<div id="%1$s" class="widget %2$s">','after_widget' => '</div>','before_title' => '<h3>','after_title' => '</h3>'));
register_sidebar(array( 'name' => 'Secondary','id' => 'secondary','description' => "Normal full width sidebar", 'before_widget' => '<div id="%1$s" class="widget %2$s">','after_widget' => '</div>','before_title' => '<h3>','after_title' => '</h3>'));
if ( ot_get_option('footer-widgets') >= '1' ) { register_sidebar(array( 'name' => 'Footer 1','id' => 'footer-1', 'description' => "Widetized footer", 'before_widget' => '<div id="%1$s" class="widget %2$s">','after_widget' => '</div>','before_title' => '<h3>','after_title' => '</h3>')); }
if ( ot_get_option('footer-widgets') >= '2' ) { register_sidebar(array( 'name' => 'Footer 2','id' => 'footer-2', 'description' => "Widetized footer", 'before_widget' => '<div id="%1$s" class="widget %2$s">','after_widget' => '</div>','before_title' => '<h3>','after_title' => '</h3>')); }
if ( ot_get_option('footer-widgets') >= '3' ) { register_sidebar(array( 'name' => 'Footer 3','id' => 'footer-3', 'description' => "Widetized footer", 'before_widget' => '<div id="%1$s" class="widget %2$s">','after_widget' => '</div>','before_title' => '<h3>','after_title' => '</h3>')); }
if ( ot_get_option('footer-widgets') >= '4' ) { register_sidebar(array( 'name' => 'Footer 4','id' => 'footer-4', 'description' => "Widetized footer", 'before_widget' => '<div id="%1$s" class="widget %2$s">','after_widget' => '</div>','before_title' => '<h3>','after_title' => '</h3>')); }
}
}
add_action( 'widgets_init', 'alx_sidebars' );
This is the codes from the alex theme register sidebar? If am I right. Just do follow steps.
01.Just copy this code from original code. Then paste it after it.
register_sidebar(array( 'name' => 'Primary','id' => 'primary','description' => "Normal full width sidebar", 'before_widget' => '<div id="%1$s" class="widget %2$s">','after_widget' => '</div>','before_title' => '<h3>','after_title' => '</h3>'));
02.Rename the name, id and description only. Don't remove class and other stuff. If you want to add new class to your own widget. Just add myClass widget %2$s like this.
03.Then create a page as using copy of sidebar-2.php. name it sidebar-3.php.
Then call to your new sidebar for act. That's it.
If you didn't give any chance to complete that. find me I'm yeshansachithak in all networks.
Thanks

Wordpress navigation Menus

I have a problem where on my WordPress theme if i have a menu assigned in Appearance->Menus the styling is okay but if i do not have one assigned i loose the styling as my navigation bar styles on the ul tag and not the div tag. The custom menu's use the ul tag with the class specified but if no menu is assigned to the location it puts the styling in the div tag.
As you can see on the picture below this is what i get outputted when menu specified
Menu Specified
this outputs
<div id="menu_dropdown">
<ul id="menu-default" class="dropdown dropdown-horizontal" style="left: 14px; top: 38px; margin-right: 0; margin-top: 0">
using the following code in functions.php
function register_my_menus() {
register_nav_menus(
array( 'primary' => __( 'Header Menu' ) )
);
}
and the following in header.php
<div id="menu_dropdown">
<?php wp_nav_menu( array( 'theme_location' => 'primary',
'menu' => '',
'container' => '',
'container_class' => '',
'container_id' => '',
'menu_class' => 'dropdown dropdown-horizontal',
'menu_id' => '',
'items_wrap' => '<ul id="%1$s" class="%2$s" style="left: 14px; top: 38px; margin-right: 0; margin-top: 0">%3$s</ul>', ) ); ?>
</div>
but the styling goes like this when no menu specified
Menu Not Specified
and outputs
<div id="menu_dropdown">
<div class="dropdown dropdown-horizontal"><ul>
</ul>
</div></div>.
This is not a styling issue as in my stylesheet i have all the correct classes e.g ul.dropdown etc.
Try instead this:
keep blank your items_wrap
and try with this one:
<div id="menu_dropdown"> <?php wp_nav_menu( array( 'theme_location' => 'primary', 'menu' => '', 'menu_class' => 'dropdown dropdown-horizontal', 'menu_id' => 'menu-default' ) ); ?> </div>
And now play with your CSS.
Thanks.

Resources