Home  >  Q&A  >  body text

Navigation bar menu parser using Bootstrap 5 and WordPress

My first attempt at integrating Bootstrap 5.2 into a WordPress site (this is just a local test). I'm doing it with this wp-bootstrap-walker I found: https://github.com/wp-bootstrap/wp-bootstrap-navwalker/

I added the "/class-wp-bootstrap-navwalker.php" file mentioned in the installation guide to functions.php:

function register_navwalker(){
            require_once get_template_directory() . '/class-wp-bootstrap-navwalker.php';
        }
        add_action( 'after_setup_theme', 'register_navwalker' ); 
        
        register_nav_menus( array(
            'primary' => __( 'Main header menu', 'mytheme' ),
        ) );

In order to use version 5, I also changed this:

'walker'          => new WP_Bootstrap_Navwalker(),
+    'walker'          => 'WP_Bootstrap_Navwalker',

So until here no errors are shown, and in the WordPress backend I can select the "Main Title Menu" menu defined in functions.php, the problem is when I select this option and go to the homepage, then everything crashes :

: Uncaught Error: Call to a member function walk() on string in C:laragonwwwmydirectorywp-includesnav-menu-template.php:622 Stack trace: #0 C:laragonwwwmydirectorywp-includesnav-menu-template.php(242): walk_nav_menu_tree(Array, 2, Object(stdClass)) #1 C:laragonwwwmydirectorywp-contentthemesmy-themeheader.php(61): wp_nav_menu(Object(stdClass)) #2 C:laragonwwwmydirectorywp-includestemplate.php(783): require_once('C:\laragon\www\...') #3 C:laragonwwwmydirectorywp-includestemplate.php(718): load_template('C:\laragon\www\...', true, Array) #4 C:laragonwwwmydirectorywp-includesgeneral-template.php(48): locate_template(Array, true, true, Array) #5 C:laragonwwwmydirectorywp-contentthemesmy-themeindex.php(8): get_header() #6 C:laragonwwwmydirectorywp-includestemplate-loader.php(106): include('C:\laragon\www\...') #7 C:laragonwwwmydirectorywp-blog-header.php(19): require_once('C:\laragon\www\...') #8 C:laragonwwwmydirectoryindex.php(17): require('C:\laragon\www\...') #9 {main} thrown in


P粉106715703P粉106715703301 days ago406

reply all(1)I'll reply

  • P粉693126115

    P粉6931261152024-01-17 10:17:31

    I solved it by writing this:

    function register_navwalker(){
        require_once get_template_directory() . '/class-wp-bootstrap-navwalker.php';
    }
    add_action( 'init', 'register_navwalker' );

    I wrote init instead of after_setup_theme to execute the function, and using wp_loaded worked fine.

    reply
    0
  • Cancelreply