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) {
["ID"]=>
int(9775)
["post_author"]=>
string(1) "1"
["post_date"]=>
string(19) "2017-01-11 00:23:04"
["post_date_gmt"]=>
string(19) "2017-01-11 05:23:04"
["post_content"]=>
string(0) ""
["post_title"]=>
string(18) "Blog"
["post_excerpt"]=>
string(0) ""
["post_status"]=>
string(7) "publish"
["comment_status"]=>
string(6) "closed"
["ping_status"]=>
string(6) "closed"
["post_password"]=>
string(0) ""
["post_name"]=>
string(16) "blog"
["to_ping"]=>
string(0) ""
["pinged"]=>
string(0) ""
["post_modified"]=>
string(19) "2017-01-25 10:38:11"
["post_modified_gmt"]=>
string(19) "2017-01-25 15:38:11"
["post_content_filtered"]=>
string(0) ""
["post_parent"]=>
int(0)
["guid"]=>
string(29) "http://localhost:8888/?p=9775"
["menu_order"]=>
int(5)
["post_type"]=>
string(13) "nav_menu_item"
["post_mime_type"]=>
string(0) ""
["comment_count"]=>
string(1) "0"
["filter"]=>
string(3) "raw"
["db_id"]=>
int(9775)
["menu_item_parent"]=>
string(4) "9779"
["object_id"]=>
string(4) "9772"
["object"]=>
string(4) "page"
["type"]=>
string(9) "post_type"
["type_label"]=>
string(4) "Page"
["url"]=>
string(34) "http://localhost:8888/blog/"
["title"]=>
string(18) "Blog"
["target"]=>
string(0) ""
["attr_title"]=>
string(0) ""
["description"]=>
string(0) ""
["classes"]=>
array(4) {
[0]=>
string(0) ""
[1]=>
string(9) "menu-item"
[2]=>
string(24) "menu-item-type-post_type"
[3]=>
string(21) "menu-item-object-page"
}
["xfn"]=>
string(0) ""
["extended_activate"]=>
string(0) ""
["highlight_menu"]=>
string(0) ""
["extended_columns"]=>
string(0) ""
["extended_heading"]=>
string(0) ""
["extended_disable_link"]=>
string(0) ""
["extended_text_chk"]=>
string(0) ""
["extended_free_text"]=>
string(0) ""
["current"]=>
bool(false)
["current_item_ancestor"]=>
bool(false)
["current_item_parent"]=>
bool(false)
}
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.
var_dump($item);
to your question, so we know what properties the item has please