• How do we can add extra custom menu item in the AMP menu using code. Let me know any hooks are available for this. The below filter is not working for AMP.

    add_filter( 'wp_nav_menu_items', 'prefix_add_menu_item', 10, 2 );
    /**
     * Add Menu Item to end of menu
     */
    function prefix_add_menu_item ( $items, $args ) {
       if( $args->theme_location == 'primary' )
           $items .=  '<li class="menu-item">...</li>';
          } 
           return $items;
    }
Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Ahmed Kaludi

    (@ahmedkaludi)

    You need to have a custom menu enabled in your themes before you can proceed any further. And need to add our own filter into wp_nav_menu_items hook. An example would look like this:

    Example code:

    add_filter( 'wp_nav_menu_items', 'your_custom_menu_item', 10, 2 );
    function your_custom_menu_item ( $items, $args ) {
        if (is_single() && $args->theme_location == 'primary') {
            $items .= '<li>Show whatever</li>';
        }
        return $items;
    }

    As you can see, you can use the conditional statements along with the argument theme_location. This allows you to target a specific menu location with any condition you want. If you don’t want the conditional statement, you don’t have to use it. Just add it to a specific menu location or vice versa.

    Thread Starter PCWP

    (@toprasobh)

    It’s working for non-AMP pages. But AMP pages these menu items are not displaying.

    Thread Starter PCWP

    (@toprasobh)

    It worked with the amp-menu item. I was trying with primary-amp-menu.

    Plugin Author Ahmed Kaludi

    (@ahmedkaludi)

    You can create that primary-amp-menu in WordPress by visiting Appearance -> Menus in your WordPress admin area > Click on create a new menu > Provide a name for your menu in the Menu Name field and then click the Save Menu button and enable that menu as “AMP” menu.

    Ref screenshot: https://prnt.sc/111jwuw

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