Pergunta

I'm trying to add a menu using a custom walker class into a page or post body and settled on using a shortcode. I don't have to use a shortcode, but I do have to embed the menu into the central portion of page/post content, and the menu must use a custom walker.

While questions like this one: https://wordpress.stackexchange.com/a/293006/67420 have a working method to generally embed a menu inside page content, trying to extend that to also pass a custom walker does fail, with this warning:

Warning: call_user_func_array() expects parameter 1 to be a valid callback, class 'new wp_bootstrap_navwalker()' not found in /.../site/wp-includes/nav-menu-template.php on line 580

This menu, using this same custom walker class, runs fine when called regularly in the header. In this certain case, I must insert the main menu into the central portion of a certain page's content, instead (and so it's not called in that page's header). Also, I've tried to explicitly include the class as suggested below by @Antti Koskinen.

Here's my shortcode:

[menu container="div" container_id="main-nav" container_class="collapse navbar-collapse justify-content-center" menu_id="false" menu_class="navbar-nav" depth="3" fallback_cb="wp_bootstrap_navwalker::fallback" walker="new wp_bootstrap_navwalker()"]

And here's my custom shortcode function:

add_shortcode( 'menu', 'se_menu_shortcode' );

function se_menu_shortcode( $atts, $content = null ) {

    extract( shortcode_atts( array(  
        'container' => null,
        'container_id' => null,
        'container_class' => null,
        'menu_id' => null,
        'menu_class' => null,
        'depth' => null,
        'fallback_cb' => null,
        'walker' => null 
    ), $atts ) );

    return wp_nav_menu( array(  
        'container' => $container,
        'container_id' => $container_id,
        'container_class' => $container_class,
        'menu_id' => $menu_id,
        'menu_class' => $menu_class,
        'depth' => $depth,
        'fallback_cb' => $fallback_cb,
        'walker' => $walker,
        'echo' => false 
    ) );
}

Appreciate any help!

Foi útil?

Solução

Update 30.6.2019

Ah, now I think understand what is going on with the code and the warning. I think it is literally trying to find a class named "new wp_bootstrap_navwalker()".

In my local sandbox WP I got the shortcode working after I changed 'walker' => $walker, to 'walker' => new $walker,. I also changed the walker parameter from walker="new wp_bootstrap_navwalker()" to walker="wp_bootstrap_navwalker"

EDIT

'walker' => new $walker, should probably be something like 'walker' => $walker ? new $walker : '', to avoid errors, if $walker is null.


If you're getting a "class not found" warning, then maybe the class file hasn't just been included/loaded when you're trying to instantiate the class. So perhaps you could try requiring/including the class file inside your shortcode function when it's needed. Something along these lines,

function se_menu_shortcode( $atts, $content = null ) {

  // extract

  if ( ! class_exists('wp_bootstrap_navwalker') ) {
    require_once 'path/to/class/definition/file.php';
  }

  // return
}

Or maybe you could try writing an autoloader for handling the requiring/including of classes automatically, https://www.php.net/manual/en/function.spl-autoload-register.php

Licenciado em: CC-BY-SA com atribuição
Não afiliado a wordpress.stackexchange
scroll top