Knowing certain details about your menus is important for managing them through the command line. For example, if you want to display a specific menu on your website you will need to know the available locations to include in the command syntax. Thanks to the list subcommand, we can gather the details we need, directly from the command line.
To learn important details about your menus when managing them using the WP-CLI, we recommend that you follow the steps described below.
Listing existing menus
The menus created can be listed using the wp menu list command.
- Log in to the server via SSH with the cPanel user who owns the website you want to work on.
- Use the cd command to access the root folder of your website eg:
cd public_html/wp
Replace public_html/wp with the actual path to the root folder of your website.
Run the following command:
wp menu list
The above command will return an output similar to:
[email protected] [~/public_html/wp]# wp menu list +---------+------------------+------------------+-----------+-------+ | term_id | name | slug | locations | count | +---------+------------------+------------------+-----------+-------+ | 8 | Primary New Menu | primary-new-menu | | 0 | | 4 | primary-nav | primary-nav | top | 0 | | 7 | primary-new | primary-new | | 0 | | 5 | social-media | social-media | social | 0 | +---------+------------------+------------------+-----------+-------+
Notice that the output displays the details of the existing menus. For example, the menu called primary-nav is assigned to the location: top. This means that the primary-nav menu will be displayed where the top menu is, according to the active theme.
Listing available menu locations
As a rule, to display a menu on your website, assign a location to the menu. Since you must first know the available location(s) to specify in the command, you must find the location details. To identify available menu locations (for the enabled theme) using WP-CLI, follow the steps below:
- Log in to the server via SSH with the cPanel user who owns the website you want to work on.
- Use the cd command to access the root folder of your website eg:
cd public_html/wp
Replace public_html/wp with the actual path to the root folder of your website.
Run the following command:
wp menu location list
The above command will return an output similar to the following, which displays all available menu locations for the activated theme:
[email protected] [~/public_html/wp]# wp menu location list +----------+-------------------+ | location | description | +----------+-------------------+ | top | Top Menu | | social | Social Links Menu | +----------+-------------------+
Notice in this output 2 available locations: top and social.
Listing menu items
You can review items included in a specific menu using the wp menu item list subcommand. To find items included in a menu using WP-CLI, follow the steps below:
- Log in to the server via SSH with the cPanel user who owns the website you want to work on.
- Use the cd command to access the root folder of your website eg:
cd public_html/wp
Replace public_html/wp with the actual path to the root folder of your website.
Run the following command:
wp menu item list primary-nav
Replace primary-nav with the name of the menu for which you want to list the items.
The command will return an output similar to the following, which displays the elements and details:
[email protected][~/public_html/wp]# wp menu item list primary-nav +-------+-----------+--------------+---------------------------------+----------+ | db_id | type | title | link | position | +-------+-----------+--------------+---------------------------------+----------+ | 38 | post_type | Home | http://w...com/wp/ | 1 | | 43 | post_type | About Us | http://w...com/wp/about-us/ | 2 | | 45 | post_type | Clients | http://w...com/wp/clients/ | 3 | | 47 | post_type | Gallery | http://w...com/wp/gallery/ | 4 | | 49 | post_type | Resume | http://w...com/wp/resume/ | 5 | | 51 | post_type | Info | http://w...com/wp/info/ | 6 | | 53 | post_type | Testimonials | http://w...com/wp/testimonials/ | 7 | | 55 | post_type | Contact Us | http://w...com/wp/contact-us/ | 8 | | 23 | taxonomy | Blog | http://w...com/wp/./blog/ | 9 | +-------+-----------+--------------+---------------------------------+----------+
Now that you're familiar with how to find menu details, you can use WP-CLI to manage menus from the command line .
For assistance in applying the above instructions on HostX servers please contact us. HostX offers the fastest WordPress hosting and can help you solve your problems.
