Opened 6 years ago
Last modified 6 years ago
#46042 new defect (bug)
wp_nav_menu always returns a container
Reported by: | thememason | Owned by: | |
---|---|---|---|
Milestone: | Awaiting Review | Priority: | normal |
Severity: | normal | Version: | |
Component: | Menus | Keywords: | |
Focuses: | Cc: |
Description
wp_nav_menu('container' => false) is returning an empty div when there is no menu items. This should return either false or an empty string. You can change the div to a different element, but you cannot remove the container.
This makes it difficult to test whether the menu is empty or not. Checking !empty($menu) no longer works like it used to.
Change History (3)
#2
@
6 years ago
has_nav_menu() is only useful if you do not intend to use the fallback function wp_page_menu called by wp_nav_menu. The problem is that if a website only uses posts and there are no actual pages, it returns an empty <div>.
Right now the only solution I've found is to use strpos to determine if an <li> item exists in what wp_nav_menu returns. This is more of a hack for now. Here's my theme file where you can see the code: https://themes.trac.wordpress.org/browser/gavel/1.0.9/header.php
#3
@
6 years ago
To reproduce the issue:
- Setup a new WP install
- Delete all pages
- Use the following code somewhere in your theme
wp_nav_menu(array( 'theme_location' => 'top', 'container' => false ));
- Inspect the results
The empty div appears to be coming from the callback wp_page_menu. The only other solution I found is to use wp_count_posts('page') as well as has_nav_menu. This still seems like a hack, I feel it should return false instead of returning an empty div.
Do you have a specific test case?
The code looks to me like having no menu would return false. But having a menu with no items will return some HTML.
Also, there is always the function
has_nav_menu()
which is a better way to test whether there is a menu or not.