• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Rahul Bodana

Blogger, Programmer & Trader

  • Code
  • Charm
  • Money
  • Write
Home » Code » PHP » WordPress » WP Themes » Add a simple navigation menu feature in a WordPress theme?

Add a simple navigation menu feature in a WordPress theme?

January 20, 2022

Navigation-Menus are important parts of your theme Since it allows visitors to navigate quickly and allows them to jump on a particular section, page, category, or post.

Just like Sidebars, before displaying Navigation menus in our theme we first have to register those in functions.php the file of our WordPress theme.

So to define our own custom navigation menu in the theme, we have to:

  1. First Register Navigation Menu in function.php file of our theme.
  2. Display the Navigation menu on desired location by editing template file.

Regsiter Menus

Here is the process that you need to follow to register navigation menu:

  1. Add following code in function.php: function register_my_menus(){ register_nav_menus( array( 'header-menu' => __('Header Menu'), 'footer-menu' => __('Footer Menu') ); ) } add_action('init', 'register_my_menus');
  2. Save the file.

Here we successfully registered two navigation menus, one to show in the header and one for display in the footer of our theme.

The next thing we need to do is edit header.php file to add Header Menu and footer.php to add Footer Menu.

Display Menus

To Display the Navigation menu on the specific location, we use wp_nav_menu().

Here is the process:

  1. Edit your header.php file. Add specifiec parameter to customize menu.
  2. Our Simple Navigation menu code would look something like this: wp_nav_menu( array( 'theme_location' => 'header-menu' ) );
  3. Do the same thing with the footer.php file for the Footer Menu: wp_nav_menu( array( 'theme_location' => 'footer-menu' ) );

You can learn more about wp_nav_menu() on official WordPress documentation – https://developer.wordpress.org/reference/functions/wp_nav_menu/.

Share this:

  • Twitter
  • Facebook
  • Reddit
  • Pinterest
  • WhatsApp
  • More
  • Pocket
  • Telegram

Related Articles

Suggested Posts:

  • WordPress Customizer API: How to Use WP Customizer Objects
  • What are the WordPress Template Files? - Explained in Detail
  • WordPress theme Custom logo: Display & Add Custom logo…
  • WordPress theme Custom widgets: How to add?
  • How to add Featured images Support in custom WordPress theme
  • How to Include CSS & JS files in WordPress themes
  • WordPress theme Conditional tags: Where and How to use

Posted in: WP Themes

Primary Sidebar

Recent Articles

  • PHP Inheritance: Concept, Override method, Modify & Final keyword
  • PHP Access Modifiers: Types of Specifiers, How to Use with Examples
  • PHP Constructor and Destructor: How to Create in PHP With Examples
  • PHP Classes & Objects: How to Create Classes and Objects in PHP
  • PHP OOP: What is OOP in PHP, Why use, PHP Classes and Objects
  • Evolution of Computers: History, Timeline, Ancient & Modern Computing devices
  • Introduction to Computer: Definition, Need & Functions of Computer

Copyright © 2022 by Rahul Bodana