Wordpress menus - php

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 )
);
?>

Related

How to make a Menu be added to a menu location by default in Wordpress

I am trying to add a menu for my new WordPress theme, what I want to happen is, the menu to be added to the registered menu location by default instead of having to go to the dashboard and selecting the menu that I want to make appear there.
I have looked at several tutorials and they all add the menu manually
This is the functions.php file which has the location registered.
register_nav_menus( array(
'main-menu' => esc_html__( 'Primary Menu', 'tester' ),
'footer-social-menu' => esc_html__( 'Social Menu', 'tester' )
));
And I call this using
<?php wp_nav_menu(array('theme_location' => 'footer-social-menu')); ?>
But WordPress still didn't show the menu by default in
Customizing ▸ Menus View ▸ All Locations
The Social Menu Area still says
---Select---
Can this be done?
(Note: The Menu is there, I just have to select it manually, i.e the menu area and the menu are successfully registered and working)
I got this to work
<?php
wp_nav_menu(array(
'theme_location' => 'footer-social-menu',
'menu' => 'Social Menu',
'fallback_cb' => false
));
?>
By Adding
'menu' => 'Social Menu',
as shown when calling the created Menu

wordpress custom menu with page thumbnail

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.

How can I programatically change menu in Wordpress?

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

Need to customize the top menu in woothemes canvas, depending if the user is logged in or not I need to show 1 menu instead of the other

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.

WP Footer Menu Not working?

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' ));

Categories