I'm using this simple function to add a dropdown indicator to my menu. However, I am getting a dropdown indicator on all children of the <li> that has the class menu-item-has-children. How do I change this so it only shows the indicator on the parent menu item?

add_filter( 'nav_menu_link_attributes', 'wpse154485_add_aria_haspopup_atts', 10, 3 );
function wpse154485_add_aria_haspopup_atts( $atts, $item, $args ) {
  if (in_array('menu-item-has-children', $item->classes)) {
    $args->link_after = "</span><span class='caret'><i class='dropdown-indicator'></i></span>";
  return $atts;

Edit: As requested, here is a vardump of one of the menu items that I do not want to show the dropdown indicator (but which currently is showing it).

object(WP_Post)#1695 (47) {
  string(1) "1"
  string(19) "2017-01-11 00:23:04"
  string(19) "2017-01-11 05:23:04"
  string(0) ""
  string(18) "Blog"
  string(0) ""
  string(7) "publish"
  string(6) "closed"
  string(6) "closed"
  string(0) ""
  string(16) "blog"
  string(0) ""
  string(0) ""
  string(19) "2017-01-25 10:38:11"
  string(19) "2017-01-25 15:38:11"
  string(0) ""
  string(29) "http://localhost:8888/?p=9775"
  string(13) "nav_menu_item"
  string(0) ""
  string(1) "0"
  string(3) "raw"
  string(4) "9779"
  string(4) "9772"
  string(4) "page"
  string(9) "post_type"
  string(4) "Page"
  string(34) "http://localhost:8888/blog/"
  string(18) "Blog"
  string(0) ""
  string(0) ""
  string(0) ""
  array(4) {
    string(0) ""
    string(9) "menu-item"
    string(24) "menu-item-type-post_type"
    string(21) "menu-item-object-page"
  string(0) ""
  string(0) ""
  string(0) ""
  string(0) ""
  string(0) ""
  string(0) ""
  string(0) ""
  string(0) ""

In this case, the Blog menu item is a child & doesn't have the 'menu-item-has-children' class. However, its parent item does have it.

  • 1
    can you add the output of var_dump($item); to your question, so we know what properties the item has please
    – David Lee
    Feb 20, 2017 at 2:36
  • @DavidLee see above (edited post)
    – timshutes
    Feb 20, 2017 at 3:15

1 Answer 1


The $args parameter are the arguments of the entire menu not of the single menu item, you can see it here in the file itself that uses and executes the filter:

@param stdClass $args An object of wp_nav_menu() arguments.

so when your IF is true (the item is a parent item), you are modifying the entire Menu arguments:

$args->link_after = "</span><span class='caret'><i class='dropdown-indicator'></i></span>";

so all menu items after the parent item would have the dropdown indicator.

If you want to add the dropdown indicator you will have to do the classic custom walker way, where you create your own class:

class Custom_Nav_Walker extends Walker_Nav_Menu{}

and override the start_el function, you will find plenty of info about it.

  • I already have a custom walker w/ custom classes. The issue is possibly a CSS one then? I thought I needed some extra markup to be added to the parent-items to do this dropdown-indicator. So I could potentially just add some CSS to .menu-item-has-children to pull this off?
    – timshutes
    Feb 23, 2017 at 15:22
  • yeah, If you already have a custom walker you can add logic there to add the dropdown indicator when the menu item is being concatenated (personal preference) and yes you can use CSS only too , my answer is to explain why the function was adding the dropdown icon to all the children.
    – David Lee
    Feb 23, 2017 at 15:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.