Wordpress custom theme - menu not showing when called in footer.php - wordpress

I'm trying to create custom website theme for Wordpress and I ran into a bit of a problem. I use two absolutely same menus in my header and footer part of website.
Calling
wp_nav_menu(array('theme_location' => 'header'));
in header.php works well, the menu prints out without any problem, but if I do the same in my footer.php file, the menu doesn't print and var_dump(wp_nav_menu(array('theme_location' => 'header'))); prints false.
I've tried some workarounds that I found on Google with modifying the functions.php file, but none of them helped me resolve that problem. My functions.php file now consists of only one line
register_nav_menus( array( 'header' => 'Header menu', 'footer' => 'Footer menu' ) );
and yes, I tried to use
wp_nav_menu(array('theme_location' => 'footer'))
as well, with same result. If I call the same function from header.php
wp_nav_menu(array('theme_location' => 'footer'))
the menu works good.

You have registered you two nav menus correctly. I always do that within my initial theme setup hook that gets hooked to the after_setup_theme hook. So I would do something like this in your functions.php:
function pietergoosen_theme_setup() {
register_nav_menus( array(
'header' => 'Header menu',
'footer' => 'Footer menu'
) );
}
add_action( 'after_setup_theme', 'pietergoosen_theme_setup' );
Keep in mind, you don't have to do it this way. The following also works
register_nav_menus( array(
'header' => 'Header menu',
'footer' => 'Footer menu'
) );
You should now see the two menus in the backend under "Appearance > Menus > Manage Locations" (Only if a menu exist)
For the sake of the footer menu, add the following code in your footer where you need to display the menu:
<nav id="footer-navigation" class="site-navigation footer-navigation" role="navigation">
<?php wp_nav_menu( array( 'theme_location' => 'footer', 'menu_class' => 'nav-menu', 'fallback_cb' => false ) ); ?>
</nav>
At this stage nothing will be displayed, and I think this is where you also get stuck at. The reason for this is that there aren't any items assigned to the menu, and if there are nothing assigned to a menu, then nothing will be displayed. So we have to insert something to be displayed.
In the backend, go to "Appearance > Menus > Edit Menus". In the "Menu Name" field, enter a name for your menu and click "Create Menu". You will now be able to add the menu in the menu screen.
You can now choose items from the left hand side to insert into your menu. You can also set the location of the menu, in this case in the footer. I've selected to display the categories in the footer. Click "Save Menu" when done.
You should now see your nav menu in the front end.
You just have to add styling to your nav bar now. You will do exactly the same for the header nav menu, accept you will add the call to the menu in the header.php. I hope you find this usefull.

So I’m producing my second WP theme and I’ve struck a problem. As well as the standard Nav menu at the top of my pages, the Footer element contains three individual sub-menus that can be loaded with individual entries. These are broken into three areas: Product Range, Industry Type and Services. The source code on footer.php is thus:
<div class="col-sm-3" style="float:left">
<h3>Product Range</h3>
<?php wp_nav_menu( array( 'footer' => 'product-range', 'container_class' => 'footer-menu' ) ); ?>
</div>
<div class="col-sm-3" style="float:left">
<h3>Industry Type</h3>
<?php wp_nav_menu( array( 'footer' => 'industry-type', 'container_class' => 'footer-menu' ) ); ?>
</div>
<div class="col-sm-3" style="float:left">
<h3>Services</h3>
<?php wp_nav_menu( array( 'footer' => 'services', 'container_class' => 'footer-menu' ) ); ?>
</div>
In functions.php I have included:
*Add footer Menus
*/
function register_my_menus() {
register_nav_menus(
array(
'product-range' => __( 'Product Range' ),
'industry-types' => __( 'Industry Types' ),
'services' => __( 'Services' )
)
);
}
add_action( 'init', 'register_my_menus' );
The output to all three menus is identical, and it is not what I want:
Product Range
COVID19 Policy
Latest and Greatest
About Us
Contact us1
Customer Service
Our Guarantee
Industry Type
COVID19 Policy
Latest and Greatest
About Us
Contact us1
Customer Service
Our Guarantee
Services
COVID19 Policy
Latest and Greatest
About Us
Contact us1
Customer Service
Our Guarantee
The top two items I added in the CMS, the remainder are automatically generated form somewhere else?
What gives?

Related

How to display custom links in WordPress with wp_nav_menu?

I'm creating an one-page custom theme. I'm struggling with a custom menu, because I want to display custom links.
I've registered menu in functions.php file
function register_my_menus() {
register_nav_menus(
array(
'header-menu' => __( 'Header Menu' ),
)
);
}
add_action( 'init', 'register_my_menus' );
Then, I've displayed in header.php file like this:
<?php wp_nav_menu( array(
'theme_location' => 'top-menu',
'container' => 'ul',
'menu_class'=> 'd-flex main-menu'
) );
?>
I've added custom classes for ul tag, and that is okay. Navigation works but just for pages. When I create a page, a link for that page shows up in navigation. However, after creating custom links they don't appear in navigation.
I'm searching the Stachoverflow for some quiet time, but without success. How to resolve a described issue? Do you have an idea?
Thank you for your time.
You have register header menu with header-menu key and trying to get it by key of top-menu which is not possible. You have to use the header-menu key to add and get a header menu. we have updated the code. you can use this code.
<?php
wp_nav_menu( array(
'theme_location' => 'header-menu',
'container' => 'ul',
'menu_class'=> 'd-flex main-menu'
) );
?>

How to display a custom menu in Wordpress?

I currently have two different custom menus. I want the first menu "Main" to be displayed at the top of the page as the top navigation. I want the second menu "Slider" under the slider.
I have this at the top:
<?php wp_nav_menu(array('theme_location' => '','container' => '',));?>
And somehow it's picking up the links in the first menu "Main" and showing it on top. Now I want to display the links from the 2nd menu under the slider
<?php register_nav_menu( 'Slider', 'Under Slider Navigation' ); ?>
<?php wp_nav_menu(array('theme_location' => 'Slider','container' => '',));?>
And with this, it is showing every single page I Have in the nav bar. Please help.
try the below function
function register_my_menus() {
register_nav_menus(
array(
'Slider' => __( 'Under Slider Navigation' ),
)
);
}
add_action( 'init', 'register_my_menus' );
<?php wp_nav_menu( array( 'theme_location' => 'Slider' ) ); ?>
register_nav_menus must be in array
Initialise the menu before you register it. Also, ideally this should go into functions.php
<?php function my_second_nav(){
wp_nav_menu(array('theme_location' => 'Slider','container' => '',))}
register_nav_menu( 'Slider', 'Under Slider Navigation' ); ?>
Then position it wherever you want
<?php my_second_nav(); ?>

Insert another menu into WordPress... not working at all?

I am following this guide to "Add additional support for menus in your theme":
http://codex.wordpress.org/Navigation_Menus
I followed all of the steps, but this is the result:
http://puu.sh/30bMt.png
So, it's "inserting a menu" where I told it to.... however the items on the menu do not match up with what I have in the WordPress back-end, seen here...
http://puu.sh/30bQd.png
I only inserted 4 items into the menu "Test"... but it's displaying every page, instead of the 4 items I want.
I've attempted to do research to figure out what's going on to no avail; does anybody have any insight as to why this is acting funky?
Code where I'm "registering" the additional menu support... (themes function.php):
function register_my_menus() {
register_nav_menus(
array(
'header-menu' => __( 'Header Menu' )
)
);
}
add_action( 'init', 'register_my_menus' );
Code where I'm inserting the "Header Menu" itself... (themes header.php):
<?php
wp_nav_menu( array( 'theme_location' => 'extra-menu', 'container_class' =>'my_extra_menu_class' ) );
?>
If you are trying to call 'Header Menu' then the code in your header.php should look like this:
<?php wp_nav_menu(array('theme_location' => 'header-menu', 'menu_class' => 'my_extra_menu_class')); ?>
I'm not sure where you got 'extra-menu' from but WordPress doesn't know what that is since you didn't declare that in your register_my_menus function.
I hope that helps.
Here's an example of how I've implemented multiple menus in my WordPress install:
// Register Extra Menus
function new_register_menus() {
register_nav_menus(array('Products'=>'Products Nav', 'Support'=>'Support Nav', 'Company'=>'Company Nav' , 'Footer'=>'Footer Nav'));
}
add_action( 'init' , 'new_register_menus' );
//Code in my footer.php
<?php wp_nav_menu(array('theme_location' => 'Footer', 'menu_class' => 'nav')); ?>

how to get nav menu list through menu name in wordpress

How do I get the nav menu list through the menu name in Wordpress?
I'm using the following code but it's showing all pages and is not using the custom nav menu name
<?php wp_page_menu('sort_column=ID&sort_order=desc;');?>
Thanks for helping.
you should register your menu in your functions.php file and add it to init.
register_nav_menu( 'header', __( 'Header Menu', 'twentyeleven' ) );
And you can display anywhere this menu as below :
wp_nav_menu( array( 'theme_location' => 'header','container' => false,'menu_id' => 'nav' ) );
First of all,
you should really assign the menu in your functions.php file and add it to init. Something like this should do:
function register_my_menus()
{
register_nav_menus(array( 'main-menu' => __( 'Main Menu' ) ) );
}
add_action( 'init', 'register_my_menus' );
Then in you wordpress theme you simple call it like so:
wp_nav_menu(array('menu_id' => '',
'menu_class' => '',
'container' => '',
'theme_location' => 'main-menu'
));
You can fill in classes, or menu ids you need, important thing is the "main-menu" clausole displaying menu you created.
Now you just need to go to your wordpress admin > appearence > menus, select your menu from the top tab list, and assing it to theme location, on right-hand side. Where it says "Theme Locations", just select "Main Menu" from the dropdown list, and hit save.
Hope this helps.

Why aren't my drupal 7 submenu's showing?

I can't seem to get my sub-menus to display.
I've gone to: Home » Administration » Structure » Menus
Then, I've edited the parent menu and checked the tickbox that says "Show as expanded" - but still nothing.
The code on my page.tpl.php page for the navigation I'm referring to, is as follows:
<?php
if ($page['navigation'] || $main_menu):
?>
<?php
print theme('links__system_main_menu', array(
'links' => $main_menu,
'attributes' => array(
'id' => 'nav',
'class' => array('links', 'clearfix'),
),
'heading' => array(
'text' => t('Main menu'),
'level' => 'h2',
'class' => array('element-invisible'),
),
));
?>
<?php
print render($page['navigation']);
?>
<?php
endif;
?>
What am I doing wrong?
Any help would be GREATLY appreciated.
Make sure that the parent menu "Show as expanded" attribute is checked.
Go to admin/structure/menu/item/MENU_ITEM_ID/edit, and check "Show as expanded"
Instead of using the $main_menu variable, you can use the main-menu block, which is generated with the menu.
If you put the "Main menu" block into the "Navigation" region at admin/structure/block, print render($page['navigation']) in the page.tpl.php will print out the complete menu, including its sub menu items (children).
Just make sure you tick the "Show as expanded" option in the parent menu link.
Lastly, remove the "print theme" stuff, otherwise you end up with double menu's.
the page.tpl.php will look something like this:
<?php if ($page['navigation']): ?>
<div id="navigation"><div class="section clearfix">
<?php print render($page['navigation']); ?>
</div></div><!-- /.section, /#navigation -->
<?php endif; ?>
The stark theme out-of-the-box doesn't show submenuus in it's main menu either (2011/04).
If you however go to admin/structure/blocks, and drag the menu block to the header region in the stark theme, it has submenus.
You'll also have two menus then, the original one without submenus and the new one with submenus. You can disable the original one in admin/appearance/stark/settings.
$2c,
*-pike

Resources