Wordpress: How can I add url GET parameter to my main menu items

前端 未结 4 400
醉话见心
醉话见心 2021-01-12 17:25

I\'m trying to add a URL GET parameter to one of my main menu items in Wordpress(but I don\'t know how to). So, my approach was to detect a click event on the menu item, the

4条回答
  •  耶瑟儿~
    2021-01-12 18:24

    The filter hook wp_get_nav_menu_items is used to manipulate the Nav Menus. The post_title used in the example is the title of the Menu (Navigation Label), not of the post/page.

    home nav menu

    Drop this code in your functions.php file, adjust the post_title and ?my_var=test to your needs. Note that better than functions is to create your own plugin.

    add_filter( 'wp_get_nav_menu_items','nav_items', 11, 3 );
    
    function nav_items( $items, $menu, $args ) 
    {
        if( is_admin() )
            return $items;
    
        foreach( $items as $item ) 
        {
            if( 'Home' == $item->post_title)
                $item->url .= '?my_var=test';
    
        }
        return $items;
    }
    

提交回复
热议问题