How to Add a New Footer Menu to Twenty Twelve

show more Adding a new footer menu to Twenty Twelve provides you with in-depth training on Web. Taught by Morten Rand-Hendriksen as part of the WordPress 3: Building Child Themes show less
please wait ...

Adding a new footer menu to Twenty Twelve

Pretty much all WordPress themes come with a main menu in the header, and most of these themes have the main menu wired to the WordPress menu system for easy configuration. I'd like to add an extra menu in the footer as well so that if a person has reached to bottom of the site, they can navigate to all my important pages without having to scroll back to the top of the page. Using a child theme, adding the menu like this, is very easy. To add a new menu we have to do three things. We have to add the menu to the existing menu function using function.php, we than have to add the menu to the template files, and then we have to style the menu to make it look nice.

To start off with, we need to create a new function that we can register a new nav menu in. So I am going to copy the original function that creates the original menu from the function.php file in the parent theme and then I am going to open the functions.php file in my child theme and create a new function in which I'll register the new menu. So I'll start by creating a new function. I'll call it mychildtheme_setup. And within this function I'm going to place the register_nav menu function.

Now I have to change some variables here. I'll change primary to secondary and I'll change Primary menu to Footer menu, because that's what I want. Now, this won't run on it's own. I also need to add an action that'll trigger my child theme setup so that it'll run when you open the theme. So I'll go back to the function.php file and take a look at how that's done. It's right down here, add_action( 'after_ setup_theme,' and then it's calls the function. So I'll copy that, paste it in, and then I just have to change the function that's being called to match the new function I created.

I'll save that and when I go to Menus in the backend of my site, you'll see we now have two menus: we have the Footer menu and Primary menu. This doesn't look right though, because the footer menu is listed before the header menu. That's actually because, as I explained before, the functions.php file from your child theme is called before the functions.php file from your parent theme. But we can fix it quite easily. You can delay the action that's happing here by simply going comma and putting in a number like 11.

That just means that this action will be called after everything else has been called. So when I saved this and reload my Admin page, Primary menu is on top and Footer menu is on the bottom. Now you see I have already assigned this Footer menu to the footer menu area, but if I have visit my site and scroll down to the bottom, there is no menu. That's because I still need to add the menu to my theme. Now, there are two main methods for calling a menu into a template file.

You can either call it based on the theme location--that is, the name that we put in, either primary or secondary or something else--or you can put it in based of the name the menu is given inside WordPress admin. They're used for different purposes. If you want to make a menu area where people can assign any menu they want to it, you would do it like this. You would call the theme location, because then people can assign whatever menu they want. If you want to make it easy for people to use your theme and say if you decide to make menu that's called Footer menu, it automatically appears in the location, then you would use this function instead, name.

I'm going to use the first one here, theme-location. I know where I want to put the menu. It's going to be in the footer. So logically, I have to go to my parent theme, find the footer template, and copy it over to my child theme. Now I can edit my child theme footer template and place my menu here. So I'll say nav id="footer_menu"> and then inside here I'll put that function.

And the location I gave it is in the function file, secondary. So I'll copy out that secondary and then I'll end my array, end the function, end my PHP delimiter, save it, and if everything went right, we now have a menu here at the bottom. It doesn't look very nice, but it is definitely a menu. Now we need to add styling to the menu to make it match the rest of the site. However, I already have a menu on my site that I think looks pretty nice and I don't really want the footer menu to look any different.

So instead of creating all new styles for the menu, I'm just going to cheat. I'm going to go to the header file right here and then I'm going to find the call to the menu right here. I'm simply going to copy it out and paste it in here in my footer file in place of that functionality I created. And then I'm going change the ID to footer-navigation, change to comment here at the bottom to footer-navigation, and then I'll take out these two lines of code here.

This is the title for the menu and the hidden content for people who use text-to-speech browsers. And though that's really important for you main menu, it's not really all that important for your footer menu. So I am taking it out. The reason why I'm copying this code is because I know the code carries with it all the necessary classes to make the menu use the same styles as the header menu. So all I need to do here is change the theme_location to secondary, save it, and reload my page.

When I scroll down to the bottom, we now have a nice-looking menu at the footer that's different from the top. So now you can go back to Menus and you can create two different menus--one for the header and one for the footer-- and assign them to each of them. By adding a menu to the existing menu functions, we preserved the original menu rather than overwrite it. This way you can add as many menus as you want and create custom navigation for your visitors to find what they're looking for.

Adding a new footer menu to Twenty Twelve
Video duration: 6m 24s 3h 11m Intermediate Updated Nov 27, 2012


Adding a new footer menu to Twenty Twelve provides you with in-depth training on Web. Taught by Morten Rand-Hendriksen as part of the WordPress 3: Building Child Themes

please wait ...