問題描述
我正在嘗試新增一些 ARIA 相關的東西到 wp_nav_menu 函式。為此,我使用一個自定義的 walker 類:
class Walker_Nav_Menu_With_Aria extends Walker_Nav_Menu {
function start_lvl( &$output, $depth = 0, $args = array() ) {
$indent = str_repeat("t", $depth);
$output .= "n$indent<ul class="sub-menu" role="group">n";
}
function end_lvl( &$output, $depth = 0, $args = array() ) {
$indent = str_repeat("t", $depth);
$output .= "$indent</ul>n";
}
function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
$indent = ( $depth ) ? str_repeat( "t", $depth ) : '';
$class_names = $value = '';
$classes = empty( $item->classes ) ? array() : (array) $item->classes;
$classes[] = 'menu-item-' . $item->ID;
$class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args ) );
$class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';
$id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args );
$id = $id ? ' id="' . esc_attr( $id ) . '"' : '';
// Add attributes for <li>
$li_attributes = ' role="treeitem"';
$li_attributes .= ' aria-expanded="false"';
$output .= $indent . '<li' . $id . $value . $class_names . $li_attributes .'>';
$attributes = ! empty( $item->attr_title ) ? ' title="' . esc_attr( $item->attr_title ) .'"' : '';
$attributes .= ! empty( $item->target ) ? ' target="' . esc_attr( $item->target ) .'"' : '';
$attributes .= ! empty( $item->xfn ) ? ' rel="' . esc_attr( $item->xfn ) .'"' : '';
$attributes .= ! empty( $item->url ) ? ' href="http://'%20%20%20.%20esc_attr(%20$item->url%20%20%20%20%20%20%20%20)%20.'"' : '';
// Add attributes for <a>
$attributes .= $depth == 0 ? ' tabindex="0"' : ' tabindex="-1"';
$item_output = $args->before;
$item_output .= '<a'. $attributes .'>';
$item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
$item_output .= '</a>';
$item_output .= $args->after;
$output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
}
function end_el( &$output, $item, $depth = 0, $args = array() ) {
$output .= "</li>n";
}
}
我只是 copy-pasted 原來的 Walker_Nav_Menu 類,並新增了東西,但我收到錯誤,因為 $ args 變數被處理像一個物件真的是一個陣列。但是真的很奇怪的是,這個錯誤仍然存在,即使我像原來的步行者一樣傳入:
wp_nav_menu( array(
'theme_location' => 'main-nav',
'walker' => new Walker_Nav_Menu,
) );
這些是我得到的錯誤訊息:
NOTICE: TRYING TO GET PROPERTY OF NON-OBJECT IN /USERS/RUDOLF/SITES/LOCALHOST/WP/WP-INCLUDES/NAV-MENU-TEMPLATE.PHP ON LINE 88
NOTICE: TRYING TO GET PROPERTY OF NON-OBJECT IN /USERS/RUDOLF/SITES/LOCALHOST/WP/WP-INCLUDES/NAV-MENU-TEMPLATE.PHP ON LINE 90
NOTICE: TRYING TO GET PROPERTY OF NON-OBJECT IN /USERS/RUDOLF/SITES/LOCALHOST/WP/WP-INCLUDES/NAV-MENU-TEMPLATE.PHP ON LINE 90
NOTICE: TRYING TO GET PROPERTY OF NON-OBJECT IN /USERS/RUDOLF/SITES/LOCALHOST/WP/WP-INCLUDES/NAV-MENU-TEMPLATE.PHP ON LINE 92
線條看起來像這樣 (在原始檔案!):
(88) $item_output = $args->before;
(89) $item_output .= '<a'. $attributes .'>';
(90) $item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
(91) $item_output .= '</a>';
(92) $item_output .= $args->after;
最佳解決方案
當沒有選單定義或 Appearance->Menus 的位置沒有設定選單時,我收到此錯誤。當發生這種情況時,wp_nav_menu 使用頁面助行器回退。
-
wp_nav_menu的回退 (預設) 為wp_walker_page -
使用
wp_page_menu -
使用
wp_list_pages -
使用
walk_page_tree -
其使用
Walker_Page不是Walker_Nav_Menu。
顯然兩個步行者是不相容的。我不知道為什麼它不會優雅地失敗。這似乎是我的錯誤。
在 wp-admin->Appearance->Menus 中設定了一個選單,你的程式碼可以工作。
您可以透過檢查在嘗試使用該位置之前看到分配給該位置的選單來避免錯誤。
$locations = get_nav_menu_locations();
if (0 !== $locations['main-nav']) {
wp_nav_menu( array(
'theme_location' => 'main-nav',
'walker' => new Walker_Nav_Menu_With_Aria,
) );
}
或者,如果你喜歡較少的抗組胺藥的程式碼 (謝謝 @Rarst):
if (has_nav_menu('primary')) {
wp_nav_menu( array(
'theme_location' => 'primary',
'walker' => new Walker_Nav_Menu_With_Aria,
) );
}
參考文獻
注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。