Site logo

Archived topic

adding menus to pages

3 replies · Started by doug on May 5, 2021

Viewing posts 1–4 of 4

I have a situation where i want to add different NAV menus to some pages.
I found code that can be put in functions.php
see this link for the code.
https://www.cozmoslabs.com/1170-wp_nav_menu-shortcode/
Is there a better way to do this in the theme or, should I follow its documentation and use the child theme?

Hi there,

You can play around with the default menus by filtering them with wp_nav_menu_args.

Example: If the page has id of 1, use "Home Menu". Every other page use "Another Menu".

add_filter( 'wp_nav_menu_args', function ( $args ) {
    if ( 'primary' === $args['theme_location'] ) {
        if ( is_page(1) ) {
            $args['menu'] = 'Home Menu';
        } else {
            $args['menu'] = 'Another Menu';
        }
    }
    return $args;
} );

I'm trying to add a second menu to some pages. so I figured the best way would be to do it via shortcode.
my question is, should I put the shortcode code in functions.php, use the code snippets plugin, or is there a better plugin for using a shortcode to call a menu.

Hi there,

PHP Functions such as the one for creating a shortcode can be added either via the Code Snippets plugin or in your Child Theme Functions.php.

This archived topic is closed to new replies.