WordPress Menus
A menu is a list of links which helps your visitors to move on the site. Most sites have a main menu at the top and a footer menu. There are two ways to build menus in WordPress, and which one you use depends on your theme.
Classic themes: Appearance > Menus
In a classic theme, go to Appearance > Menus.
- Click create a new menu and give it a name.
- Add pages, posts, categories or custom links from the left boxes.
- Drag the items to change the order. Drag an item a little to the right to make it a sub item (a dropdown).
- Choose a Display location (for example Primary Menu) and click Save Menu.
A menu is really a taxonomy of the type nav_menu, and each link is a post of type nav_menu_item. WP-CLI can build one for you:
$ wp post create –post_type=page –post_title="About" –post_status=publish –porcelain
5
$ wp menu create "Main Menu"
Success: Created menu 2.
$ wp menu item add-post main-menu 5 –title="About us"
Success: Menu item added.
$ wp menu item add-custom main-menu "Our Blog" http://example.com/blog/
Success: Menu item added.
$ wp menu item list main-menu –fields=db_id,title,type,link
db_id title type link
6 About us post_type http://example.com/about/
7 Our Blog custom http://example.com/blog/
Block themes: the Navigation block
Block themes like Twenty Twenty-Five do not have the Menus screen. You edit the menu in the Site Editor (Appearance > Editor > Navigation), or directly inside the Header, by selecting the Navigation block. The menu is saved as a post of the type wp_navigation:
$ wp post list –post_type=wp_navigation –post_status=any –fields=ID,post_title,post_type
ID post_title post_type
4 Navigation wp_navigation
Good menu practices
- Keep the main menu short, five to seven items.
- Use clear names that tell where the link goes.
- Do not open normal links in a new tab.
- Make sure it also works on the phone.
| Exercises & Assignments |
|---|
| No Content Found. |
| Interview Questions & Answers |
|---|
| No Content Found. |