php - Walker_Nav_Menu() for top level links only
I have a menu in the WordPress backend that looks like this:
What we do
Solutions
Solution 1
Solution 2
Services
Service 1
Contact
Answer
Solution:
Normally, at the start_lvl
and end_lvl
you would output <ul>
and </ul>
tags respectively to start and end a new list.
Every item in your menu consists of an anchor tag wrapped inside a list item:
<li><a href="target-url">Target</a></li>
In your code you only have an 'if' statement around the code that adds the list item tags to the output.
On the last line of your start_el
function you still add the anchor.
Instead of wrapping an if statement around the parts where you want to output markup, my advice would be to add the following line of code as the first line of your start_el
function:
if($depth !== 0) { return; }
That way you skip execution of code for items that are not in the first level.
Source