$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>theme customizer - Visible Edit Shortcut for WordPress menu that uses nav walker|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

theme customizer - Visible Edit Shortcut for WordPress menu that uses nav walker

matteradmin8PV0评论

I am working on a WordPress theme. In the main navigation menu I had to use the following code to apply Bootstrap4 styles on the menu items (from here).

register_nav_menus( array(
    'menu-1' => esc_html__( 'Primary', 'theme-textdomain' ),
) );

// Now, you can display the menu in your theme via below PHP code addition in the header.php file of your theme.

<nav class="navbar navbar-expand-lg navbar-light bg-light">
        <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbar-content" aria-controls="navbar-content" aria-expanded="false" aria-label="<?php esc_html_e( 'Toggle Navigation', 'theme-textdomain' ); ?>">
            <span class="navbar-toggler-icon"></span>
        </button>

        <div class="collapse navbar-collapse" id="navbar-content">
            <?php
            wp_nav_menu( array(
                'theme_location' => 'menu-1',
                'menu_id'        => 'primary-menu',
                'container'      => false,
                'depth'          => 2,
                'menu_class'     => 'navbar-nav ml-auto',
                'walker'         => new Bootstrap_NavWalker(),
                'fallback_cb'    => 'Bootstrap_NavWalker::fallback',
            ) );
            ?>
        </div>
</nav>

If I dont use the nav-walker the edit shortcut is visible and works for the navigation menu.

But, when I use the nav-walker the button goes away.

I tried adding this code in customiser

 $wp_customize->selective_refresh->add_partial('primarymenu', array(
        'selector' => '#site-navigation',
        'render_callback' => 'asensio_customize_partial_primarymenu',
    ));

This has no effect.

I am working on a WordPress theme. In the main navigation menu I had to use the following code to apply Bootstrap4 styles on the menu items (from here).

register_nav_menus( array(
    'menu-1' => esc_html__( 'Primary', 'theme-textdomain' ),
) );

// Now, you can display the menu in your theme via below PHP code addition in the header.php file of your theme.

<nav class="navbar navbar-expand-lg navbar-light bg-light">
        <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbar-content" aria-controls="navbar-content" aria-expanded="false" aria-label="<?php esc_html_e( 'Toggle Navigation', 'theme-textdomain' ); ?>">
            <span class="navbar-toggler-icon"></span>
        </button>

        <div class="collapse navbar-collapse" id="navbar-content">
            <?php
            wp_nav_menu( array(
                'theme_location' => 'menu-1',
                'menu_id'        => 'primary-menu',
                'container'      => false,
                'depth'          => 2,
                'menu_class'     => 'navbar-nav ml-auto',
                'walker'         => new Bootstrap_NavWalker(),
                'fallback_cb'    => 'Bootstrap_NavWalker::fallback',
            ) );
            ?>
        </div>
</nav>

If I dont use the nav-walker the edit shortcut is visible and works for the navigation menu.

But, when I use the nav-walker the button goes away.

I tried adding this code in customiser

 $wp_customize->selective_refresh->add_partial('primarymenu', array(
        'selector' => '#site-navigation',
        'render_callback' => 'asensio_customize_partial_primarymenu',
    ));

This has no effect.

Share Improve this question edited Oct 6, 2017 at 4:35 Mark Kaplun 23.8k7 gold badges43 silver badges65 bronze badges asked Oct 4, 2017 at 11:12 Shafayat AlamShafayat Alam 1496 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

Nav menus in will use selective refresh by default when they are output into the page via wp_nav_menu() but with a few restrictions on the arguments. For the exact requirements on the $args, see WP_Customize_Nav_Menus::filter_wp_nav_menu_args().

In short, if you're using a custom walker, then you need to pass it as a class name string instead of as an instantiated object. For example, instead of:

wp_nav_menu( array(
    // ...
    'walker' => new My_Custom_Walker(),
) );

Do this instead:

wp_nav_menu( array(
    // ...
    'walker' => 'My_Custom_Walker',
) );
Post a comment

comment list (0)

  1. No comments so far