• Resolved koolmind

    (@koolmind)


    Good evening everybody. I need to add a custom attribute to my menu items.

    This is what I believe I have to do… but nothing is added.

    add_filter( 'nav_menu_link_attributes', 'add_role_attribute_to_menu' );
    function add_role_attribute_to_menu( $atts ) {
      $atts['role'] = 'button';
      return $atts;
    }

    I’m using WP 5.6 and Astra free theme.

    What’s the matter? Maybe I have to include the add_filter inside another call?

    Thanks

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Support Team Brainstorm Force

    (@brainstormteam)

    Hello @koolmind

    There is some syntactical mistake in the above filter.

    Kindly check this below filter:

    add_filter( 'hfe_nav_menu_attrs', 'add_role_attribute_to_menu' );
    function add_role_attribute_to_menu( $atts ) {
        $atts .= ' role=' . 'button';
        return $atts;
    }

    Kindly refer to the screenshot( at our end )

    Please let us know in case of any further help.

    Regards,
    Rahul.

    Thread Starter koolmind

    (@koolmind)

    Thank you very much!
    Is there a way to apply the filter only on first level menu items?
    Can I only work on $atts variable, or there’s the chance to get also the a $item variable to work with?

    Thanks again.

    Plugin Support Team Brainstorm Force

    (@brainstormteam)

    Hello @koolmind

    It won’t be possible to apply the attributes to the first level item only.

    Also, only attributes can be changed, not the items.

    Please let us know in case of any further help.

    Regards,
    Rahul.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Adding attribute to menu items’ is closed to new replies.