Wordpress child theme, adding secondary menu - wordpress

<?php
if ( function_exists('has_nav_menu') && has_nav_menu('primary-menu') ) {
wp_nav_menu( array( 'sort_column' => 'menu_order', 'container' => 'ul', 'menu_id' => 'main-nav', 'menu_class' => 'nav fl', 'theme_location' => 'primary-menu' ) );
} else {
?>
I'm trying to add a secondary menu from Wordpress menu management in my functions.php of my child theme for Woothemes Canvas.
I figure there's a way to add it to the array above but I can't get it to work. Thoughts?

Jason, you first need to register your 'new' (secondary) menu with register_nav_menu() like:
add_action( 'init', 'register_my_menu' );
function register_my_menu() {
register_nav_menu( 'secondary-menu', __( 'Secondary Menu' ) );
}
You do this in your theme's functions.php file.
Then you're able to call that menu in your template files. To use your code above, you'll probably want something like:
if ( function_exists('has_nav_menu') && has_nav_menu('secondary-menu') ) {
wp_nav_menu( array( 'sort_column' => 'menu_order', 'container' => 'ul', 'menu_id' => 'secondary-nav', 'menu_class' => 'nav fl', 'theme_location' => 'secondary-menu' ) );
}
or maybe
if ( function_exists('has_nav_menu') && has_nav_menu('primary-menu') && has_nav_menu('secondary-menu') ) {
wp_nav_menu( array( 'sort_column' => 'menu_order', 'container' => 'ul', 'menu_id' => 'main-nav', 'menu_class' => 'nav fl', 'theme_location' => 'primary-menu' ) );
wp_nav_menu( array( 'sort_column' => 'menu_order', 'container' => 'ul', 'menu_id' => 'secondary-nav', 'menu_class' => 'nav fl', 'theme_location' => 'secondary-menu' ) );
}
The second one will output both menus if they both exist, the first one will probably be used in addition to the one you posted in your initial question.

But in my case, I did not use the init action, just put the menu register function in my child theme's function.php file
register_nav_menu( 'footer', 'Footer Menu' );

Related

Removing WordPress Wrapper Div When container => false Fails

I'm having troubles removing the WordPress wrapper div from my wp_nav_menu(). I've passed container => false to my menu args but the div keeps displaying. I'm using the following arguments:
$defaults = array(
'container' => false,
'theme_location' => 'menu',
'menu_class' => 'main-nav',
);
wp_nav_menu( $defaults );
I have also declared the menu in my functions.php file.
register_nav_menus( array(
'main-nav' => __( 'Main Nav', 'ldsshop' ),
));
I have used these arguments with previous themes with no problems, but in this instance the wrapper keeps displaying, and I'm at a point where I need some extra eyes and help, no doubt I've missed something.
Thanking you all in advance,
Stu :)
Use container empty like this
'container' => '',
So it will like this
$defaults = array(
'container' => '',
'theme_location' => 'menu',
'menu_class' => 'main-nav',
);
wp_nav_menu( $defaults );
It will work, even it's working on my hand.
Update About Solved This Question
Try to the following using array in wp_nav_menu
wp_nav_menu( array(
'theme_location' => 'menu',
'container' => '',
'menu_class' => 'main-nav'
) );
use same '' about container if false not to work
Here is an article finding some helpful
See more in the codex https://developer.wordpress.org/reference/functions/wp_nav_menu/
In my case "container" => false doesn't work, It only works when you write "items_wrap" => "%3$s" but it removes all wraps including <ul> if you want to remove <div> and have your menu wrapped in <ul> I recommend to do this way
wp_nav_menu(array(
'theme_location' => 'header_menu',
'menu' => 'header_menu',
'items_wrap' => '<ul>%3$s</ul>',
'container' => false,
'menu_class' => 'nav',
'list_item_class' => 'nav-item',
'link_class' => 'nav-link',
'menu_id' => 'menu-main-menu'
));

Wordpress wp nav menu doesn't work

I don't understand why it's impossible for me to get my custom menu on my front-end page
I use:
<?php
wp_nav_menu( array(
'theme_location' => 'jdsMainMenu',
'depth' => '1',
'menu_class' => 'jds-main-menu-list',
'menu_id' => 'jds-nav',
'container' => '',
'items_wrap' => '<ul class="jds-main-menu-list">%3$s</ul>'
));
?>
I have my menu in WordPress back office.
I try to make:
var_dump(get_registered_nav_menus());
My menu is available: but nothing display with wp_nav_menu...
Maybe some one have an idea...
I am sure you are using correct ID for theme location
screenshot
function register_my_menus() { register_nav_menus(
array(
'jdsMainMenu' => __( 'Navigation Menu' )
)
);
}
add_action( 'init', 'register_my_menus' );
wp_nav_menu( array(
'theme_location' => 'jdsMainMenu',
'depth' => '1',
'menu_class' => 'jds-main-menu-list',
'menu_id' => 'jds-nav',
'container' => '',
'items_wrap' => '<ul class="jds-main-menu-list">%3$s</ul>'
));

Wordpress Primary menu not updating after reordering menu items

In functions.php, I registered primary menu like this-
<?php
register_nav_menus( array(
'menu-1' => esc_html__( 'Primary', 'intralink' ),
) );
?>
And in Homepage I have used below code:
<?php
wp_nav_menu( array(
'theme_location' => 'Primary',
'container' => 'ul',
'container_class' => 'navbar-collapse collapse',
'menu_class' => 'nav navbar-nav navbar-right'
));
?>
Then after creating a menu from wordpress Menus, nothing happens? Maybe it's not drag drop problem, maybe it's something I did wrong.
Thanks
Theme location should be 'menu-1'
It should look like the following
wp_nav_menu( array(
'theme_location' => 'menu-1',
'container' => 'ul',
'container_class' => 'navbar-collapse collapse',
'menu_class' => 'nav navbar-nav navbar-right'
));
Hope this will solve your issue.

Menu gets wrap in Div - wordpress

Working on Wordpress menu: with Bootstrap
My ul li gets wrap within Ul and creates Div.
Screenshot attached:
It seems it automatically adds classes to menu li and and prevents the bootstrap styles and classes
My wp menu function :
/**
* Get our wp_nav_menu() fallback, wp_page_menu(), to show a home link.
*/
function register_my_menus() {
register_nav_menus(
array(
'header-menu' => __( 'Header Menu' ),
'extra-menu' => __( 'Extra Menu' )
)
);
}
add_action( 'init', 'register_my_menus' );
Try changing in the theme template the nav menu function like this :
<?php
$defaults = array(
'theme_location' => '',
'menu' => '',
'container' => '', // <- This empty = no container
'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 );
?>
Source : https://codex.wordpress.org/Function_Reference/wp_nav_menu
Take a look at the container index of the defaults array.

Why do i have duplicated menu on wordpress?

Here i am creating 2 menu sites "Main" and "Central"
if ( function_exists( 'register_nav_menus' ) ) {
register_nav_menus(
array(
'main' => 'Main',
'central' => 'Central'
)
);
}
when i am creating menus on wordpress aperience, menus
and i am puting them on their sites
PRS menu on Main
and central on Central
When i have incerted my menus on web
<?php wp_nav_menu( array('menu' => 'Main','menu_class' => 'menu' )); ?>
<?php wp_nav_menu( array('menu' => 'Central','menu_class' => 'menu' )); ?>
But on my web i am seyng "central" menu 2 times and PRS dont apear.
What is the problem and what solution cn i find?
Try single Register this way & then move on to array - add code below to functions.php:
WP Codex register_nav_menu
<?php
add_action( 'after_setup_theme', 'register_my_menu' );
function register_my_menu() {
register_nav_menu( 'primary', 'Primary Menu' );
}
?>
Display menu on page - add to header.php:
WP Codex wp_nav_menu
<?php
$defaults = array(
'theme_location' => 'primary',
'menu' => 'Primary 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 );
?>
Tip - both of your entries have a 'menu' class:
Your code:
<?php wp_nav_menu( array('menu' => 'Main','menu_class' => 'menu' )); ?>
Change to:
<?php wp_nav_menu( array('menu' => 'Main' )); ?>

Resources