我第一次尝试将 Bootstrap 5.2 集成到 WordPress 站点中(这只是本地测试)。 我正在通过我发现的这个 wp-bootstrap-walker 来做到这一点: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: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粉6931261152024-01-17 10:17:31
我写这个解决了它:
function register_navwalker(){ require_once get_template_directory() . '/class-wp-bootstrap-navwalker.php'; } add_action( 'init', 'register_navwalker' );
我编写了 init 而不是 after_setup_theme 来执行该函数,并且使用 wp_loaded 效果很好。