I've created one menu in Norwegian menu_no and one in English menu_en.
I can see that my theme supports only one menu, but I'm not planning on using more that one menu at a time. So when user selects English language, how can I change the active menu?
I've not found anything on Google and I can't find the right function in nav-menu.php.
UPDATE
I found quite a simple solution for my problem. I just had to think a bit differently. In my functions.phpI added this code:
add_action('init', 'register_menus');
function register_menus(){
register_nav_menus( array(
'menu_no' => 'Norwegian menu',
'menu_en' => 'English menu',
) );
}
and in my header.php file I use this code:
global $lang;
$args = array(
'theme_location' => 'menu_'.$lang,
'container' => false
);
<?php wp_nav_menu($args); ?>
Voila. I'll post it as answer later - if not anyone else comes up with a better idea.
Here is How You Can Do That.
Past this code to function.php file
// Register Navigation Menus
function custom_navigation_menus() {
$locations = array(
'first_menu' => __( 'Menu for English language', 'text_domain' ),
'second_menu' => __( 'Menu for Other Language', 'text_domain' ),
);
register_nav_menus( $locations );
}
// Hook into the 'init' action
add_action( 'init', 'custom_navigation_menus' );
You can customize the above code.
Now use wp_nav_menu function to display anywhere you want like this
<?php
wp_nav_menu( array(
'menu' => 'primary',
'theme_location' => 'first_menu',
);
?>
<?php
wp_nav_menu( array(
'menu' => 'secondary',
'theme_location' => 'second_menu',
);
?>
You can display each menu in different places. Now you to Option in you menu Primary and Secondary
Related
I am trying to pull menu on my header.php using following code:
wp_nav_menu( array(
'theme_location' => 'main-menu',
'menu_id' => 'main-menu',
) );
And in my admin login Appearance > Menu, main-menu is in following order:
Home
About
Tours
Pages
Contact Us
Help Desk
Gallery
Destinations
Blog
Booking
But in header the order is jumbled and even doesn't show the submenu also.
Order on header
About
Pages
Tours
Booking
Home
Blog
Destinations
Help me out! I even tried using order_by/sort_column, they didn't work for me.
I think Below code is what you looking for.
Add your Menu Name Instead of 'Main menu'
<?php
$main_menu = wp_get_nav_menu_items('Main menu');
$customize_arr = array();
if(!empty($main_menu)){
foreach($main_menu as $mm){
$mm = (array) $mm;
if($mm['menu_item_parent'] == 0){
foreach($main_menu as $sm){
$sm = (array) $sm;
if($mm['ID'] == $sm['menu_item_parent']){
(array)$mm['submenu'][] = $sm;
}
}
$customize_array[] = $mm;
}
}
}
?>
In $customize_array you will get all the menus with sub-menu you just have to do foreach loop with your html code.
100% Working code..I hope the Code Will help You!
If you developed the theme from scratch then add the following as an index of your array
'orderby' => 'menu_order'
Would be somthing like below
wp_nav_menu( array(
'theme_location' => 'main-menu',
'orderby' => 'menu_order'
'menu_id' => 'main-menu',
) );
If you are working on an already developed them then the issue is with 'theme_location' index of the array, so change it to 'theme_location' => 'primary' it will be something like below:
wp_nav_menu( array(
'theme_location' => 'primary',
'menu_id' => 'main-menu',
) );
I am trying to get a menu with few links, and show their/link/page featured image. following is my code for functions.php.
function register_my_menus() {
register_nav_menus(
array(
'main-menu' => __( 'Main Menu' ),
'useful-links' => __( 'Userful Links' )
)
);
}
add_action( 'init', 'register_my_menus' );
and this is where i am calling it in the page.
<?php wp_nav_menu( array( 'theme_location' => 'useful-links', 'sort_column' => 'menu_order', 'container_class' => 'useful-links', 'menu'=>'Posts Menu' ) ); ?>
i know something is missing but kindly advice. my page look and feel allow 6 links and their images.
thanks
This should be in your functions.php to register the menu within WordPress.
if (!function_exists('wp_basicTemplateSetup')) {
function wp_basicTemplateSetup() {
// Register the Main Menus into WordPress
register_nav_menus(array(
'primary' => __('Main Menu', 'theme-name'),
));
}
}
add_action('after_setup_theme', 'wp_basicTemplateSetup');
In your theme, where the menu is sitting you need to tell the menu function, which menu to look for. So..:
// Variable that tells WordPress which menu to use, look at "Theme Location"
// Put this variable either at the top of your theme, of right *before* the function wp_nav_menu
$mainmenu = array(
'theme_location' => 'primary',
'container' => false,
'menu_class' => 'some-menu-css-class',
'depth' => 2
);
// The WordPress menu function
<?php wp_nav_menu($mainmenu); ?>
These 3 components are necessary to get your WordPress menu working.
i m having a problem with menus.
I had a template that wasnt originaly for WP.
so i integrated it.
when i started the site there was only one menu which works great.
i added <?php wp_nav_menu('primary'); ?> and its all ok
Now i need to add another one (custom links in the header section)
sooo i used this method
register_nav_menus( array(
'header' => 'Header menu',
'footer' => 'Footer menu'
) );
and recieved new locations for menus.
for the second menu i added
<?php wp_nav_menu( array( 'theme_location' => 'header', 'menu_class' => 'nav-menu', 'fallback_cb' => false ) ); ?>
now the problem:
there is a primary menu (it has few items)
and the custom menu ( its currently empty)
when i add new page to custom menu its being shown in both primary and custom menus.
if i delete all the items from custom menu, primary menu items are back in place...
what is the problem?
Thank you.
If I understand correctly, it looks like you are pointing to the wrong registered menu in the wp_nav_menu(). Specifically in the theme_location.
When you are building the "second menu" which is the "footer", theme_location should be footer since that is the name you gave it in the register_nav_menus().
<?php wp_nav_menu(
array( 'theme_location' => 'footer', // Change 'header' to 'footer'
'menu_class' => 'nav-menu',
'fallback_cb' => false )
);
?>
I've already created a canvas child-theme. I added my custom page woo-hooks.class.php in the canvas-child folder in which i copied all the code from the original and added here:
$this->hooks['header'] = array(
'woo_top' => array(
'content' => '',
'shortcodes' => 0,
'php' =>'
the following code:
if ( is_user_logged_in() ) {
wp_nav_menu( array( "theme_location" => "top-menu-loggedin" ) );
} else {
wp_nav_menu( array( "theme_location" => "top-menu-loggedout" ) );
}
the code is like it doesn't even exists, I know I've probably mistaken what I have to do, but i couldn't find a clear guide anywhere on how to do this, just some code in some forums with no context about how and where to act (the code i added I found it in a forum, but I guess i didn't add it in the right place). The filter and hooks guides of WP don't make clear WHERE and HOW to call the custom filters and hooks and how to actually make them interact with the existing code... I'm going nuts, please help.
Top menu customization can be performed in header section, that is in child theme's 'header.php' file or in other file, specifying 'theme-location' parameter.
You can write below code, in your child theme,
if (is_user_logged_in()){
wp_nav_menu(array(
'menu_class' => 'member-menu',
'menu' => 'Members',
'theme_location' => 'primary');
);
}else{
wp_nav_menu(array(
'menu_class' => 'top-menu',
'menu' => 'Members non Logged in',
'theme_location' => 'primary')
);
}
Here, 'theme-location' parameter is specifies location of Menu and 'menu' specifies name of Menu.
I have been working on a site to change from HTML To Wordpress CMS...so got completed everything...Now stuck in one problem..and that is that I have declared the Main Navigation and Footer Navigation correctly (according to me) in functions.php file as :
/* Register Nav Main Menu */
register_nav_menus (array('main-nav' => 'Main Navigation'));
register_nav_menus (array('footer-nav' => 'Footer Navigation'));
while I am calling the both menus in correct way as for main-nav I am calling it in header.php as :
wp_nav_menu( array( 'menu' => 'main-nav' ));
while calling the footer-nav as following in footer.php as :
wp_nav_menu( array( 'menu' => 'footer-nav' ));
but in both places only main-nav is shown...so what did I do wrong please?..Need a hand to fix it...!
Waiting for your replies..!
Here is live link to the site as : http://www.huntedhunter.com/minaterra/
try changing:
wp_nav_menu( array( 'menu' => 'footer-nav' ));
to
wp_nav_menu( array( 'theme_location' => 'footer-nav' ));