php 引导程序5和wordpress导航栏步行者

ecbunoof  于 2022-11-28  发布在  PHP
关注(0)|答案(1)|浏览(145)

我第一次尝试将Bootstrap 5.2集成到一个WordPress站点(这只是一个本地测试)。我正在通过这个wp引导程序步行者做它,我发现:https://github.com/wp-bootstrap/wp-bootstrap-navwalker/
我添加了'/class-wp-bootstrap-navwalker.php'文件,它在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' ),
        ) );

为了使用第5个版本,我还对此进行了更改:

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

所以直到这里没有错误显示,也在WordPress的后端我可以选择'主标题菜单'菜单中定义的functions.php,问题是当我选择这个选项,我去主页面,然后一切崩溃:

: Uncaught Error: Call to a member function walk() on string in C:\laragon\www\mydirectory\wp-includes\nav-menu-template.php:622 Stack trace: #0 C:\laragon\www\mydirectory\wp-includes\nav-menu-template.php(242): walk_nav_menu_tree(Array, 2, Object(stdClass)) #1 C:\laragon\www\mydirectory\wp-content\themes\my-theme\header.php(61): wp_nav_menu(Object(stdClass)) #2 C:\laragon\www\mydirectory\wp-includes\template.php(783): require_once('C:\\laragon\\www\\...') #3 C:\laragon\www\mydirectory\wp-includes\template.php(718): load_template('C:\\laragon\\www\\...', true, Array) #4 C:\laragon\www\mydirectory\wp-includes\general-template.php(48): locate_template(Array, true, true, Array) #5 C:\laragon\www\mydirectory\wp-content\themes\my-theme\index.php(8): get_header() #6 C:\laragon\www\mydirectory\wp-includes\template-loader.php(106): include('C:\\laragon\\www\\...') #7 C:\laragon\www\mydirectory\wp-blog-header.php(19): require_once('C:\\laragon\\www\\...') #8 C:\laragon\www\mydirectory\index.php(17): require('C:\\laragon\\www\\...') #9 {main} thrown in
4dbbbstv

4dbbbstv1#

我写了这样的话来解决这个问题:

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

我编写了init而不是after_setup_theme来执行该函数,同样与wp_loaded一起工作也很好。

相关问题