wp_nav_menu() always null - wordpress

No matter what parameters I pass to wp_nav_menu it always comes out blank (NULL).
Here's how I registered the nav support in functions.php:
function mytheme_addmenus() {
add_theme_support( 'nav-menus' );
if ( function_exists( 'register_nav_menus' ) ) {
register_nav_menus(
array(
'rightsidebar' => 'Right Sidebar Menu'
)
);
}
}
add_action( 'init', 'mytheme_addmenus' );
Then I create a new menu in the admin and assign it to the "Right Sidebar Menu" location. The ID of the menu is 5, the name is test.
None of the following return anything (NULL to be precise). I'm calling it in sidebar.php:
wp_nav_menu('menu=test');
or
wp_nav_menu(array('menu' => 'test'));
or
wp_nav_menu($a = array('menu' => 'test'));
or
wp_nav_menu('menu_id=5');
or
wp_nav_menu('menu=5');
or
wp_nav_menu('menu=rightsidebar');
However, when I call the following I get a valid object containing the menu meta data:
wp_get_nav_menu_object('test')
So clearly 'test' is a valid menu= parameter.
Am I missing something? Please help!
PS. Wordpress version 3.3. Theme is super basic built from scratch.

you are calling the menu by menu id (which you are saying is 5) but you are registering it by a theme location.
Try
wp_nav_menu( array( 'theme_location' => 'rightsidebar' ));

wp_nav_menu echoes the reult instead of returning it. If you want to return the menu string, try using :
$args['echo'] = false;

Related

Unable to get wordpress register_nav_menus working

I am building my 1st theme, and got stucked with showing menu option on admin's screen under appearance. I refered various support thread but none of the solution is working for me.
Codes in function.php
<?php
//Create Nav menu
if (function_exists(register_nav_menus)) {
register_nav_menus( array('primary' => 'Header Navigation') );
}
?>
Codes in header.php
<?php
wp_nav_menu( array( 'container_class' => 'main-nav' , 'container'=>'nav') );
?>
I am executing this on localserver and wordpress version 4.0
image of header.php
http://i.imgur.com/ziPGH2G.png
image of function.php
http://i.imgur.com/ica5VJx.png
Thats all of it.
Please help me out.
First rename function.php to functions.php , note the the "s". http://codex.wordpress.org/Functions_File_Explained
Second try registering the menu inside a hook like init or after_theme_setup
add_action( 'after_setup_theme', 'register_my_menu' );
function register_my_menu() {
register_nav_menu( 'primary', 'Header Navigation' );
}
You're missing some arguments (and a comma in your array). You need to pass in a menu name:
$args = array(
'menu' => 'primary',
'container' => 'nav',
'container_class' => 'main-nav',
);
wp_nav_menu( $args );
Also be aware that this menu must exist in the admin, and must be assigned to the menu location you just created.

wordpress, custom menu is created but does not show up in the top head part in the admin panel

My theme has several custom menus. I can manage them in the WP admin panel.
But look at the picture. You see 2 menus already created - Footer Menu and Header. But in my case those 2 names do not appear so I cannot do anything with them later.
Code I used:
add_action( 'init', 'menus_all' );
function menus_all() { register_nav_menus( array(
'menu1' => _( 'menu1 loc'),
'menu2' => _( 'menu2 loc'),
) ); }
How to make the menu names appear ?
register_nav_menu goes straight in your functions.php file, without any hook:
register_nav_menus( array(
'menu1' => _( 'menu1 loc'),
'menu2' => _( 'menu2 loc'),
));

wp_nav_menu not working on category pages on wordpress

I created a couple of custom menus on wordpress using the appearance menu.
I want to display the right menu according to the page that I'm viewing. I wrote the script below in the header.php file in the seems to be working fine. It's pulling the correct menu according to the page that I'm visiting with the exception of the category pages.
When I'm on a category page, the wp_nav_menu function falls back to the fallback function, indicating that the menu doesn't exist?!
I was looking around and the solution that kept coming up was adding the following code but it doesnt seem to work.
<?php
wp_nav_menu('container_class=menu-header&theme_location=primary');
?>
Here is the full code that I added to the header.php file:
<?php
switch( $master_page ) {
case 'about':
wp_nav_menu(array(
'menu' => 'about',
'fallback_cb' => 'get_cat',
));
break;
case 'offer':
wp_nav_menu(array(
'menu' => 'offer',
'fallback_cb' => 'get_cat',
));
break;
}
?>
I'm really frustrated at this point. Any ideas?
I don't see where you are assigning a value to $master_page, so at this point it is just an empty variable.
You need to look into the is_page() and is_category() functions in WordPress and use if statements rather than a switch.
<?php
if( is_page( 'about' ) ) {
wp_nav_menu(array(
'menu' => 'about',
'fallback_cb' => 'get_cat',
));
} else {
...
}
?>

Wordpress menu - adding items to a menu without selecting a primary menu manually

I am making a theme for wordpress that makes it in to an app.
I would like the menu items below to show up when runned in header.php - however it only shows up if I activate a new menu with at least one item as primary menu.
I want a user to be able to install wordpress and then just activate the theme (without having to config a menu manually).
Can I add the items below without having to do the procedure of activating a primary menu? Or any other good workaround.
add_filter( 'wp_nav_menu_items', 'add_links', 10, 2 );
function add_links( $items, $args ) {
$items = "";
$items .= '<li>Hem</li>';
$items .= '<li>About-us</li>';
return $items;
}
wp_nav_menu( array( 'theme_location' => 'primary', 'menu_class' => 'nav-menu' ) );
Take a look at the fallback_cb parameter of the wp_nav_menu function. Here's a nice explanation: https://wordpress.stackexchange.com/a/64526/25765

add menu item to the dashboard menu of wordpress

I am trying the following syntax to add menu-item on dashboard:
<?php add_menu_page( $page_title, $menu_title, $capability, $menu_slug, $function, $icon_url, $position ); ?>
But, I am confused where to add this code.
Whether to add it in my theme's functions.php or my plugin's functions.php ?
Or any other way to add custom menu-item on the dashboard of the wordpress logged in as admin ?
I want to add custom menu item like the following image:
What you see in the screenshot is the screen of a Custom Post Type. In the documentation you will find an example code on how to add such a screen.
About where to place the code - it depends. Do you want to be able to use this Custom Post Type in other themes, or you will only need it in this theme?
If you want to use it in other themes as well, put the code in your plugin's code.
If you want to use it only in this theme, put it in your theme's functions.php.
And in case you still want to add a custom menu page, here you will find examples on what is the proper way to use this function. What you should note, is that the call to add_menu_page() should be done inside a function that is run on the admin_menu action.
Here's an example working code with WP 3.4.2
function register_custom_menu_page() {
add_menu_page('custom menu title', 'custom menu', 'add_users', 'custompage', '_custom_menu_page', null, 6);
}
add_action('admin_menu', 'register_custom_menu_page');
function _custom_menu_page(){
echo "Admin Page Test";
}
This is a perfect answer.
add_action( 'init', 'create_post_type' );
function create_post_type() {
register_post_type( 'acme_product',
array(
'labels' => array(
'name' => __( 'Products' ),
'singular_name' => __( 'Product' )
),
'public' => true,
'has_archive' => true,
)
);
}

Resources