Monday, August 8, 2022
HomeWeb DevelopmentMake a Drop-Down Menu in WordPress

Make a Drop-Down Menu in WordPress


Do you wish to create a drop-down menu in WordPress? You have come to the suitable place! On this tutorial I am going to educate you how you can create an expert drop-down menu design.

Navigation menus are having a little bit of a second within the highlight. From burger menus for cell by mega menus for shops to sticky menus for enhanced person expertise, there’s an important alternative in the way in which you may current your navigation menu in your WordPress website.

However what if you wish to create a simple drop-down menu for WordPress with a number of top-level objects and a few extra objects that drop down from them when the person hovers over them?

Earlier than you begin stepping into coding superior menus like mega menus and burger menus, it is a good suggestion to learn to create a drop-down menu. This may are available in helpful on extra websites than you may think (not each website wants a flowery menu), and it gives you the muse you must begin constructing extra superior menus.

In case you’d slightly watch our video on making a drop-down menu in HTML for WordPress, simply press play and get began.


On this tutorial, I will present you how you can create a drop-down menu in your WordPress theme, utilizing CSS to focus on the HTML that is output by the WordPress menu perform. That is designed to be utilized in a theme you are coding your self, and never for a third-party theme, which is able to have already got its personal menu. Nonetheless, in the event you’re working with a third-party theme whose menu is not drop-down and also you wish to add this, then you definately’ll must create a baby theme and add your menu code to that.

This is What You may Be taught in This Drop-Down Menu Design Tutorial

  • Perceive how WordPress drop-down menus work by its built-in menu performance.
  • Get accustomed to code so you may learn to make a drop-down menu in HTML. 
  • Making your drop-down menu design mobile-friendly. 

What You may Have to Create a Drop-Down Navigation Menu in HTML on WordPress

To comply with together with this tutorial, you will want:

  • a improvement set up of WordPress
  • a theme you are coding your self, or a baby theme of a third-party theme if you wish to modify the menu
  • a code editor

1. WordPress’s Constructed-in Menu Performance

The very first thing you will want to know is how WordPress drop-down menus work. Not like for static websites, menus aren’t hard-coded into your website. As an alternative, WordPress makes use of a PHP perform to question the database and fetch navigation menu objects, then show them within the right construction.

Every merchandise in your navigation menu is definitely a submit within the wp_posts desk in your database—not a traditional submit, however a particular type of submit that is used only for navigation menu objects, with its personal metadata together with the textual content to be displayed and the goal of the hyperlink.

In your theme, open up the header.php file. It is best to be capable to discover this line:

Your perform might look somewhat completely different relying on the parameters, however let’s break down the instance above and see what every component does:

  • wp_nav_menu() is the perform that fetches a navigation menu and outputs it.
  • The parameters are then wrapped in an array.
  • container_class is the CSS class that will probably be given to the container through which the menu is wrapped. On this case, it is main-nav. That is what we’ll be focusing on with our CSS in a while.
  • theme_location => main tells WordPress that that is the first navigation. If I create a menu within the admin screens and examine the Main field, then this menu will probably be used for this spot within the code.

Typically you would possibly wish to add a navigation menu elsewhere in your theme, for instance within the footer, through which case you do not wish to use theme_location => main. You possibly can solely use this for one menu. However it’s possible you’ll wish to use further parameters, which you could find within the WordPress handbook web page on wp_nav_menu().

This is the checkbox for the first navigation within the Menus admin display screen:

Primary navigation settingPrimary navigation settingPrimary navigation setting

 2. Code Output by the wp_nav_menu() Operate

Earlier than we will add CSS for our dropdown menu, it helps to be accustomed to the code that WordPress generates for menus.

This is a typical drop-down menu instance for a small enterprise, proven within the Menus admin display screen:

Navigation menu in admin screenNavigation menu in admin screenNavigation menu in admin screen

Now this is the drop-down navigation menu in HTML:

Making a drop-down menu in HTML consists of some code we have to perceive earlier than beginning with our drop-down menu for WordPress: 

  • A div with the main-nav class, outlined within the wp_nav_menu() perform.
  • Inside that, a ul with the ID menu-navbar and the category menu. These are defaults outlined by WordPress.
  • Inside that, a lot of li parts, every with the category of menu-item menu-item-type-post_type, plus different courses particular to the kind of submit that the menu merchandise results in and the state of that menu merchandise on the time. Each additionally has a novel ID, with a quantity similar to the submit ID of the navigation menu merchandise within the database.
  • Inside one of many li parts is one other ul with its personal li parts inside—the second-level menu objects. It is this that we wish to drop down when the person hovers over the top-level menu merchandise.

Coding the CSS for our Drop-Down Menu

So now we all know what’s being output by WordPress, we will decide which parts we wish to goal with our CSS for the drop-down menu.

We wish to obtain a few issues:

  • When the web page is opened, the second-level menu objects are hidden.
  • When the person hovers over a top-level merchandise, the second-level objects under it seem.

Hiding the Second-Degree Gadgets by Default

In your theme’s stylesheet, begin by hiding the second-level objects by default.

Add this:

This may conceal the ul component inside one other ul component contained in the main-nav component. It will not conceal a top-level ul component, nevertheless, because it requires one ul to be nested inside one other ul contained in the menu.

Now, in the event you open the web page and attempt to view the second-level objects, it will not be potential—they’re going to be hidden. Let’s repair that.

Drop-Down Menu Design: Creating Second-Degree Gadgets Seem on Hover

Now we have to be sure that the ul nested contained in the top-level li will probably be displayed when the top-level li is hovered over.

Add this to your stylesheet:

Now, if you hover your mouse over the top-level merchandise, the record beneath it’s going to seem. However you will discover that it will not show the way in which you need it. Particularly, it will likely be pushing down the content material under the menu. We would like it to seem as if it is floating on high of the content material. To repair that, we have to add some structure styling to our ul ul component.

Including Format Styling to the Second-Degree Listing

Open your stylesheet and discover the road with show: none in it. Edit that block so as to add structure styling:

You additionally want to provide the top-level record merchandise relative positioning:

Let’s check out what a easy drop-down menu code has:

  • place: absolute provides the second-level record absolute positioning, taking it out of the circulate of parts within the web page. For the higher-level merchandise, place: relative places the top-level record within the circulate of the web page and permits for a completely positioned component to be positioned inside it.
  • high: 3em positions the highest of the record relative to the highest of the component it is inside, specifically the top-level record merchandise. This 3em worth displays the peak of the top-level navigation bar. Edit yours in case your top-level navigation has a distinct peak.
  • left: 0 locations the record to the left, relative to the merchandise above it.
  • z-index: 99999 defines the place the component sits in a three-dimensional mannequin of the web page. A excessive worth of 99999 ensures that it’s displayed on high of every little thing else.
  • The remaining code provides the record width and likewise provides show styling to it, together with a shadow to make it look as if it is on high of the web page.

Now let’s check out what we see after we hover over the top-level merchandise:

Hovering over menuHovering over menuHovering over menu
It is a drop-down menu instance of how the hover impact seems.

It really works! Once I hover over the top-level merchandise, the drop-down menu is now displayed.

3. Making Your Drop-Down Menu Cell-Pleasant

The easy drop-down menu code above is nice for the desktop model of the positioning, however the actuality is that most individuals will probably be visiting your website on a cell phone.

The menu right here is just too massive to suit onto a small display screen, so the very best resolution is to edit the CSS for our drop-down menu on small screens and use some JavaScript to create a burger menu.

Right here’s how you can do it.

Including a Menu Icon to the Banner of a Drop-Down Menu on WordPress

First, add the icon that folks might want to faucet on to entry the menu on a small display screen.

Add this to the header.php file, within the place the place you need the menu icon to go:

That can output the burger image, utilizing the HTML code for the image, inside a component with a category we’ll use to cover it on bigger screens.

Including the CSS for the Burger Menu

Now you must add the CSS to your stylesheet. First, conceal the icon on bigger screens:

Now inside a media question, add the CSS for the menu:

Word that you simply’ll must edit this in the event you’re utilizing completely different courses and IDs in your theme.

Including the JavaScript

The ultimate step is so as to add a script to make the menu seem when a person faucets on the icon. Create a folder in your theme referred to as scripts, and inside that, a brand new file referred to as burger-menu.js, and add this to it:

Now make certain the script known as by the theme. In your theme’s capabilities.php file, add a perform to enqueue the script:

Now save all of your recordsdata, and also you’ll have a burger menu on small screens.

Burger menuBurger menuBurger menu

Drop-Down Menus Are Helpful for Small, Multi-Degree Menus

When your website wants a menu with a number of ranges however you do not want a whole lot of hyperlinks exterior your top-level menu, a drop-down menu is the best option to obtain this. The location I’ve used to display this solely has one merchandise in its menu with different objects under it, and there are solely three of these. Utilizing a mega menu could be overkill, and a single-level menu would not permit me to show every little thing I would like.

That is It! That is Make a Drop-Down Menu in HTML

With the ability to add a menu like this to your themes gives you extra flexibility together with your menus and improve the person expertise. And you are able to do it with only a few strains of CSS.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments