我第一次嘗試將 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
我寫這個解決了它:
我寫了 init 而不是 after_setup_theme 來執行該函數,並且使用 wp_loaded 效果很好。