You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在WordPress中用二级菜单替换主菜单并在navigation2.php中沿用主菜单样式类?

How to Replace Primary Menu with Custom Secondary Menu in WordPress (Medizin Theme)

Got it, let's get this sorted for you. You're looking to use your custom-registered secondary menu in navigation2.php while retaining the primary menu's styling—here's how to adjust your code correctly:

Step 1: Fix Menu Registration (functions.php)

First, there's a typo in your menu registration (secoundary should be secondary). Correcting this ensures the menu slug matches when we call it later:

function wpb_custom_new_menu() {
    register_nav_menu('secondary', __( 'Secondary' ));
}
add_action( 'init', 'wpb_custom_new_menu' );

Step 2: Update navigation2.php to Load Your Custom Menu

Instead of calling the theme's built-in primary menu function (Medizin::menu_primary()), we'll use WordPress's native wp_nav_menu() to load your secondary menu, while keeping all the primary menu's CSS classes so the styling stays identical:

<div id="page-navigation" <?php Medizin::navigation_class(); ?>>
    <nav id="menu" class="menu menu--primary">
        <?php
        wp_nav_menu( array(
            'theme_location' => 'secondary', // Matches the slug we registered in functions.php
            'container'      => false, // Keep the existing nav structure
            'menu_class'     => 'menu__container', // Match the theme's primary menu inner class
        ) );
        ?>
    </nav>
</div>

What This Does:

  • theme_location => 'secondary': Tells WordPress to load the menu you registered instead of the primary one.
  • container => false: Ensures we don't add extra wrapper divs that might break the theme's existing layout.
  • menu_class => 'menu__container': Uses the same inner menu class as the primary menu, so all styling rules apply directly.

Step 3: Verify Header Setup (header.php)

Your current header code already includes <?php get_template_part( 'template-parts/navigation2' ); ?>, which is perfect—this will load your modified secondary menu in the header alongside (or instead of, if you remove the original navigation template part) the primary menu.

Once you've made these changes, go to Appearance > Menus in your WordPress admin, assign your desired menu items to the "Secondary" menu location, and you're good to go!

内容的提问来源于stack exchange,提问作者miko

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 20:13:11