• Hello, I posted this question to this board, because it looks to me, that WP core doesn’t support this feature, I’m not sure if it should work, but it would be nice if someone with knowledge could clarify this.

    All cases that I found on the internet deal with the new secondary menu that should be registered and used in a child theme. In my case I have a parent theme with two registered menus and I have a child theme, where I want to use both of them. There is no problem with the ‘primary’ menu, somehow even without registering it works in a child theme out of the box.

    The problem is that secondary menu will not be generated when called from the child theme. For details please see this question at wp stackexchange:

    https://wordpress.stackexchange.com/questions/177414/use-secondary-menu-of-parent-theme-in-child-theme

    Thank you!

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    Sorry, I’m a little fuzzy on what you are really doing. AFAICT, you are simply trying to display your parent theme’s secondary menu on a child theme’s template. Is that right?

    That should be no problem. Be sure you have the ‘theme_location’ argument correct, find the registration call in the parent’s code to determine the proper keyname to use.

    For the sake of finding an easy workaround, try registering a third location and add your menu to that. Use the new location in your wp_nav_menu() call.

    I know you tried re-registering the parent’s secondary menu, I’d be afraid that could cause some sort of name collision. I’m suggesting a completely new location in which to place an existing menu. That new location is your child template.

    FYI, all of your parent’s code still runs under a child theme, so that’s why you don’t need to register the locations, and why you shouldn’t try to re-register the same again.

    As I said, you should be able to display the menu associated with the parent’s secondary location in a child template. I don’t see any issues with the code you posted, but you might try stripping out all unnecessary code and arguments to eliminate any possible complicating factors. If your menu still fails to display, I’d speculate there is some sort of plugin conflict causing problems. Try deactivating all plugins and see if the menu appears. If that doesn’t help, try specifying one of the default twenty something themes as the parent of your child (if it’s not already of course).

    Thread Starter Tomas Mackevicius

    (@tomasm)

    @bcworkz thank you very much for the answer!

    It’s 2:30AM and I finally got to the root of the issue :)))

    There was a conflict with the function in child’s functions.php that inserts a standard html5 search form into primary menu:

    function tinyframeworkfullwidth_add_search_to_wp_menu ( $items, $args ) {
    	if( 'primary' === $args -> theme_location ) {
    	return $items . '<li class="menu-item menu-item-search">' . get_search_form(false) . '</li>';
    	}
    }
    add_filter( 'wp_nav_menu_items','tinyframeworkfullwidth_add_search_to_wp_menu',10,2 );

    I’m still puzzled why it causes a conflict, because when I was using hard-coded html4 search form it didn’t interfere with secondary menu. And IF clearly checks only for the primary menu, right?:

    function tinyforgeiichild_add_search_to_wp_menu ( $items, $args ) {
    	if( 'primary' === $args -> theme_location ) {
    	$items .= '<li class="menu-item menu-item-search">';
    	$items .= '<form role="search" method="get" class="searchform" action="' . home_url( '/' ) . '"><label><span class="screen-reader-text">Search for:</span></label>';
    	$items .= '<input class="text_input" type="search" value="" name="s" id="s" placeholder="Search …" title="Search for:" /><input type="submit" class="searchsubmit" value="Search" /></form>';
    	$items .= '</li>';
    	}
    	return $items;
    }
    add_filter( 'wp_nav_menu_items','tinyforgeiichild_add_search_to_wp_menu',10,2 );

    Edit. I modified new function a bit and now everything works! ??

    But still, I don’t know why it interferes with secondary nav…

    function tinyframeworkchild_add_search_to_wp_menu ( $items, $args ) {
    	if( 'primary' === $args -> theme_location ) {
    	$items .= '<li class="menu-item menu-item-search">' . get_search_form(false) . '</li>';
    	}
    	return $items;
    }
    add_filter( 'wp_nav_menu_items','tinyframeworkchild_add_search_to_wp_menu',10,2 );
    Moderator bcworkz

    (@bcworkz)

    The filter fires for all calls to wp_nav_menu(). In the first snippet, if the call is NOT for ‘primary’, nothing is returned. Thus $items is essentially empty and the function echoes that empty nothing out, resulting in nothing except for ‘primary’ menus.

    One of the first rules for filter callbacks, with rare exception, is that some value must be returned under all conditions. Typically, the default state would be to return the passed parameter unchanged. Having the return statement inside an IF structure violates this.

    Good work tracking this down! You got off lucky only being up to 2:30. You’re like me, once you feel like you’re on the right track, you cannot let it go until it’s resolved. I’ve seen the sun rise many times doing this ??

    Thread Starter Tomas Mackevicius

    (@tomasm)

    ?? thank you one more time for clear explanation!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Use secondary menu of parent theme in child theme’ is closed to new replies.