Basic Tutorial On Creating Dynamic Menu Items In WordPress Pages
WordPress is well-known for providing a wide spectrum of functionality, enabling users to setup their website consisting of a rich suite of features. One great feature of WordPress is its menu system, which helps in managing a website menu in an effortless manner. In fact, the menu system became even better with the release of WordPress 3.0 version. [m2leep] The new menu system made the life of developers a bit easier, allowing them to do a lot more with their website menu in an easy manner. However, beginner’s often find the process of creating a dynamic menu on a WordPress page or post pretty challenging. This tutorial is intended to provide you with knowledge as to how you can create and display dynamic menus on your WordPress site. But, some of you may be interested in knowing about what is a dynamic menu and its importance. So, let’s first talk about the same.
Whenever you’ll access any WordPress website theme, you’ll notice that it contains a hierarchy of menu items, dynamic menus help maintain that hierarchy on all the web pages. The best aspect about a dynamic menu is that it isn’t only added in a site’s header section, but can be also be embedded in the footer and sidebar section as well. What’s more? The menu items of a dynamic menu are displayed on your website subpage automatically – when that subpage is being created in your WordPress site admin area. So, you don’t have to spend additional time in creating a menu item for all the subpages separately, as it will automatically show up in your main navigation bar when a subpage gets created. You can easily choose the location where you would like your dynamic menu items to be displayed from the WordPress admin panel dashboard. Sadly, many themes don’t allow to add menu items dynamically, so let’s proceed and see how you can create and display your own choice of dynamic menu items in your WordPress site.
Begin by logging into your site admin dashboard, and navigate to Appearance->Menus. This will open up a window as shown below: Next, simply add the name of the menu items – in the “Menu Name” section – that you would like to be a part of your dynamic menu. For instance, I’m creating a menu named as “Main Menu”. Once you’ve entered the name of the menu of your choice, just click on the “Create Menu” button and the below given window will pop up: Make sure to check the Auto Add Pages option (as shown above) from the Menu Settings section. This will ensure that with the creation of a subpage, a menu item for that same page will automatically get created. But, keep in mind that you’ll have to determine the location where you want the dynamic menu item(s) to be displayed. To fulfill this objective, simply check the option corresponding to the “Theme locations” (just like the above screenshot).
Note: Every WordPress theme won’t provide the same support to create a dynamic menu. Some of the WP themes will make you add dynamic menu item(s) in the main navigation menu, while a few other themes will allow adding menu in the footer. But, unfortunately, you may not be able to find one that displays dynamic menu items in the sidebar.
Let us now proceed to adding dynamic menu items in the sidebar section of WordPress default Twenty Fifteen theme. For this, you first need to activate the theme, and then browse to Appearance menu and click on the Menu tab. Doing so, you’ll be able to see a window that looks like: So, when you’ll look at the window, you can see that two different options are visible in the Menu Settings, namely: Primary Menu and Social Links Menu. However, to show your menu items in the sidebar (or say widgetized region) of your site, you need to write a few lines of code (to be included in your theme’s functions.php file).
Using wp_nav_menu() (a built-in WordPress function), you can create dynamic menu item(s). This function helps in making a call to the newly created menu. In addition, it even enables you to display the custom menu. The function is passed several parameters, but the two most important ones are theme_location and menu as discussed below:
- theme_location: it makes a call to the menu that is hooked to some given location. But if that location doesn’t exist, you can choose to create a new one. To do so, you’ll need to register the menu’s location using the register_nav_menu() function. For example, to show up your newly created menu (i.e. dynamic menu items) in the sidebar use the following line of code (which needs to be added in your theme functions file):
- Menu: this parameter is used to make calls to the dynamic menu (using ID, slug or name).
Do you want to learn the process of creating and displaying menu items dynamically in your WordPress install, then reading this tutorial will prove a good starting point for you.