Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases now! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
WordPress 5 Cookbook

You're reading from   WordPress 5 Cookbook Actionable solutions to common problems when building websites with WordPress

Arrow left icon
Product type Paperback
Published in Mar 2020
Publisher Packt
ISBN-13 9781838986506
Length 660 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Rakhitha Nimesh Ratnayake Rakhitha Nimesh Ratnayake
Author Profile Icon Rakhitha Nimesh Ratnayake
Rakhitha Nimesh Ratnayake
Arrow right icon
View More author details
Toc

Table of Contents (16) Chapters Close

Preface 1. Setting Up WordPress and Customizing Settings 2. Customizing Theme Design and Layout FREE CHAPTER 3. Using Plugins and Widgets 4. Publishing Site Content with the Gutenberg Editor 5. Managing Users and Permissions 6. Setting up a Blogging and Editorial Workflow 7. WordPress as an Application Framework 8. Improving Usability and Interactivity 9. Building E-Commerce Sites with WooComerce 10. Troubleshooting WordPress 11. Handling Performance and Maintenance 12. Improving Site Security 13. Promoting and Monetizing the Site 14. Other Books You May Enjoy Appendix

Styling navigation menus

Simplified and clear navigation menus make the site more accessible to the user. The menu should be designed to let the user visit the most important parts of the site with a single click and let the user know what they are viewing at any given moment. WordPress generates a simple list-based menu by default. We can use the theme to modify the menu functionality and create stylish menus.

In this recipe, we are going to look at the process of changing the existing menu design using custom styles for menus.

Getting ready

We need to create a navigation menu before we get started with styling. Follow these steps to create a navigation menu for the Twenty Twenty child theme:

  1. Log in to the WordPress Dashboard as an administrator.
  2. Click the Appearance menu.
  3. Click the Menus option. You will see the following screen:
  1. Click the create a new menu link.
  2. Add a menu name of Primary Menu and click the Create Menu button.
  3. Select some menu items from the left-hand panel and click Add to Menu.
  4. Click the Save menu button.

Now, we have created a menu that can be used for custom styling.

How to do it...

We have created a basic menu for the Twenty Twenty theme. In this recipe, we are going to style each menu item using custom classes. To do this, follow these steps:

  1. Log in to the WordPress Dashboard as an administrator.
  2. Click on the Appearance menu.
  3. Click on the Menu option. You will see a screen similar to the following showing the menu we created in the Getting ready section of this recipe:
  1. Click the Screen Options menu at the top to view the enabled/disabled settings for the menu section. You will see the following screen:
  1. Click the CSS Classes option if it's not selected in your menu section. This will enable the CSS Class setting for each menu item.
  2. Click the down arrow of one of the menu items to open the menu item and its settings. The following screenshot shows a menu item's settings with the CSS Classes option enabled:
  1. Add a custom name(s) for the CSS class(es) to this menu item. We can add multiple classes by separating them with spaces. In this case, we have to add a class called wpc-home-menu-item. The class names that we added in this section will be used later in CSS files to add styles.
  2. Click the Save Menu button to save the settings.
  3. Click the Manage Locations tab to get to the following screen:
  1. Set the Primary Menu we created in the Getting ready section to Desktop Horizontal Menu.
  2. Click the Save Changes button.
  3. Open wp-content/themes/twentytwentychild/style.css with a text or code editor.
  4. Add the following CSS styles at the end of the file to customize the styles of the home menu item:
.wpc-home-menu-item{
background: #1862a2;
padding: 5px;
border: 1px solid;
}
.wpc-home-menu-item a{
color : #fff !important;
}
  1. Save the file.

Now, you can visit the home page of the site to see the modified menu item. In this case, we have changed the default styles of the home menu item. The following screenshot shows the home menu item with custom styling:

Usually, menu item styling is done to highlight specific menu items among others. We can use this kind of menu item-specific style for calls to action buttons or links to external sites.

We can follow this process for each menu item and add custom styles of our choice using custom CSS classes.

How it works...

Once we define CSS classes using the CSS Classes option in the menu item, it will be added to the <li> element of each menu item, along with the built-in CSS classes. You can check the applied CSS classes by using the browser inspection tool on the menu item. The modified menu item code will look something similar to the following:

<li id="menu-item-12" class=" wpc-home-menu-item menu-item menu-item-type-post_type menu-item-object-page menu-item-12 is-focused"><a href="http://www.yoursite.com/home/">Home</a></li>

As you can see, the element contains the wpc-home-menu-item class, followed by built-in CSS classes such as menu-item and menu-item-post_type. Then, we need to add the CSS code from Step 13 to the style.css file of the theme.

We used custom CSS to highlight the home menu item with a background color, padding, and a border. Also, we changed the color of the menu link to #000 to match the background color of the menu item.

Once styles for this class have been added to the style.css file of the theme, the changes will be shown on the menu. So, the menu item will be highlighted among the other menu items. We can also use the same class on multiple or all menu items to style a set of menu items differently from others.

There's more...

In this recipe, we looked at the process of using our own CSS on menu items to customize the styles of the default menu. However, we have the option of using existing menu plugins to completely customize and build a stylish menu without writing a single line of code. Let's take a look at how we can use an existing plugin to completely change the styles and effects of the entire menu:

  1. Go to the Plugins | Add New section from the Dashboard
  2. Search for Max Mega Menu in the Search Plugins field
  3. Install and activate the plugin
  4. Click Appearance | Menus. Here, you will see a new settings section alongside the menu for Max Mega Menu, as shown in the following screenshot:
  1. Click the Enable checkbox
  2. Change the events and settings based on your preference
  3. Hover over any menu item and click the Mega Menu button
  4. Click the icon tab and select an icon
  5. Follow steps 7 and 8 for each menu item
  6. Click the Save Menu button

Now, you can visit the home page of the site to see the modified menu that was generated by the Max Mega Menu plugin. You will see a stylish menu similar to the following with menu icons, animations, and a professional design:

We can use such plugins to easily convert the entire menu design or design of specific menu items.

Before moving on to the next recipe, remove the custom CSS for the home menu item we added in the style.css file of the child theme and deactivate the Max Mega Menu plugin.
You have been reading a chapter from
WordPress 5 Cookbook
Published in: Mar 2020
Publisher: Packt
ISBN-13: 9781838986506
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime