WordPress offers an amazing set of features, helping users set up their website equipped with enhanced functionality. Among the rich set of features, menu system is one great WordPress feature you should know about, allowing you to manage your website's menu in an efficient manner.
With the release of WordPress version 3.0, the menu system was revamped and custom navigation menu feature was added to help developers do a lot of things with their site's menu. But, many beginner's still find the dynamic menu creation process in WordPress a daunting task.
This post is written with the purpose to help beginners learn the process of creating dynamic menu items in their WordPress website pages. But, before jumping on that part, let us first have a brief overview of dynamic menu and why you need to create a custom dynamic menu.
What Is Dynamic Menu?
As soon as you access any WordPress theme, you'll see that its navigation menu contains a list of items (or you can say, a hierarchy of menu and sub-menu items). Dynamic menus are the ones that help in managing that hierarchy on each web page. What's more? A dynamic menu not only gets added in the header section of a website, but can also be added in the footer or or other section in your site.
Why You Need To Create Custom Dynamic Menu?
In a WordPress site, the dynamic menu items gets displayed on the live site web pages (ideally sub pages), when that page gets created in the site's admin area. This saves you from spending your time in creating menu items for each and every sub page individually.
From your site's admin dashboard, you can select the location where your dynamic menu items need to be displayed. But, unfortunately not all of the WordPress themes lets you add menu items within the web pages dynamically. This is why you need to create your own custom dynamic menu items in your WordPress site.
How To Create Dynamic Menu in WordPress Pages?
The very first thing you need to do is to get logged in into your website's admin dashboard. And then, browse to Appearance ?Menus from the left side of the your admin dashboard screen. This will result in opening the window such as the one shown below:
Now, you just have to enter the name of the menu items (to be displayed in your dynamic menu) in the section named as "Menu Name". Let's say, for example, you would like to create a menu with the name: Main Menu. Simply enter the menu name and hit the "Create Menu" button. This will open up a window as follows:
Next, you need to ensure that the "Auto Add Pages" option is checked (as shown in the image above) in the "Menu Settings" section. Doing so, will make sure that when a subpage will be created, a dynamic menu item for that page will be built automatically. However, remember to specify the exact location where you would want to show your dynamic menu item(s). For this, you simply need to go to "Theme locations" and check any one of the options corresponding to it.
Adding and Displaying WordPress Theme in Sidebar
Usually most of the WordPress theme won't provide you with the same support for creating a dynamic menu. While a few themes will allow you to add dynamic menu item(s) in the navigation menu bar at the top of the site, others will let you add the menu in the footer. Sadly, you may find it difficult to find a theme, allowing you to add and display dynamic menu items in the theme's sidebar section.
In this section, we'll view the process to add dynamic menu items in the WordPress default "Twenty Fifteen" theme sidebar area. To do so, you'll first have to activate the theme. After installation, move to the Appearance menu and then hit the Menu tab, and the window as shown below will pop up:
Now, as you can see there are two different options provided in the Menu Settings, such as: Primary Menu and Social Links Menu. But, you cannot see any option to add menu items in the sidebar. To achieve this objective, you will need to write a few lines of code that will be added to your theme functions file.
How To Display Dynamic Menus in a WordPress Page?
You need to use the WordPress built-in function wp_nav_menu() for creating dynamic menu item(s). Apart from creating menu items dynamically, the function also helps in displaying the newly created custom menu. A lot of parameters are being passed to this function, however, there are two important ones you need to know about such as:
1. theme_location: help in making a call to the menu hooked to any particular location. In case, the location doesn't exist or is not specified, you can create a new one by registering it with the help of register_nav_menu() function. For example, for presenting your menu items in your theme's sidebar add the below give line of code in the theme functions.php file:
<?php wp_nav_menu(array('theme_location'=>'sidebar')); ?>
2. Menu: it is used to make calls to the dynamic menu via ID, slug, etc.
Hope that going through this post will make you understand dynamic menu and why it is needed. Additionally, this post will serve as a resourceful guide to help you understand the process of creating dynamic menu items in your WordPress installation.