Tôi đã nghiên cứu và thử RẤT NHIỀU thứ nhưng không có kết quả...Tôi chỉ muốn thiết lập động một lớp 'hoạt động' dựa trên url của trang
Bản mới nhất tôi đã thử có vẻ rất rõ ràng và súc tích nhưng không được – http. //www. siphon-tiếp thị. com/nav-test/nav-test. html
Ai đó có thể VUI LÒNG giúp đỡ?
len
24 Tháng Tư, 2017 lúc 10. 44 giờ chiều
Beverleyh
người tham gia
buồn cười bạn nên hỏi. Chỉ cần viết điều này vào thứ Sáu http. //Blog. fofwebdesign. đồng. uk/40-highlight-the-menu-button-for-the-current-web-page
25 Tháng Tư, 2017 lúc 3. 40 giờ sáng
Atelierbram
người tham gia
@beverleyh đã đánh dấu. ;]
@lprintz có lỗi javaScript, hiển thị nếu bạn mở bảng điều khiển trong DevTools. jQuery không được bao gồm
Nhưng bên cạnh các giải pháp javaScript, có nhiều cách hơn để lột da con mèo này khi sử dụng kết xuất phía máy chủ, chẳng hạn như trong PHP [các khung công tác PHP như WordPress sẽ đặt lớp
wp_nav_menu[ array[
'menu' => 'Something custom walker',
'walker' => new WPDocs_Walker_Nav_Menu[]
] ];
/**
* Custom walker class.
*/
class WPDocs_Walker_Nav_Menu extends Walker_Nav_Menu {
/**
* Starts the list before the elements are added.
*
* Adds classes to the unordered list sub-menus.
*
* @param string $output Passed by reference. Used to append additional content.
* @param int $depth Depth of menu item. Used for padding.
* @param array $args An array of arguments. @see wp_nav_menu[]
*/
function start_lvl[ &$output, $depth = 0, $args = array[] ] {
// Depth-dependent classes.
$indent = [ $depth > 0 ? str_repeat[ "\t", $depth ] : '' ]; // code indent
$display_depth = [ $depth + 1]; // because it counts the first submenu as 0
$classes = array[
'sub-menu',
[ $display_depth % 2 ? 'menu-odd' : 'menu-even' ],
[ $display_depth >=2 ? 'sub-sub-menu' : '' ],
'menu-depth-' . $display_depth
];
$class_names = implode[ ' ', $classes ];
// Build HTML for output.
$output .= "\n" . $indent . '' . "\n";
}
/**
* Start the element output.
*
* Adds main/sub-classes to the list items and links.
*
* @param string $output Passed by reference. Used to append additional content.
* @param object $item Menu item data object.
* @param int $depth Depth of menu item. Used for padding.
* @param array $args An array of arguments. @see wp_nav_menu[]
* @param int $id Current item ID.
*/
function start_el[ &$output, $item, $depth = 0, $args = array[], $id = 0 ] {
global $wp_query;
$indent = [ $depth > 0 ? str_repeat[ "\t", $depth ] : '' ]; // code indent
// Depth-dependent classes.
$depth_classes = array[
[ $depth == 0 ? 'main-menu-item' : 'sub-menu-item' ],
[ $depth >=2 ? 'sub-sub-menu-item' : '' ],
[ $depth % 2 ? 'menu-item-odd' : 'menu-item-even' ],
'menu-item-depth-' . $depth
];
$depth_class_names = esc_attr[ implode[ ' ', $depth_classes ] ];
// Passed classes.
$classes = empty[ $item->classes ] ? array[] : [array] $item->classes;
$class_names = esc_attr[ implode[ ' ', apply_filters[ 'nav_menu_css_class', array_filter[ $classes ], $item ] ] ];
// Build HTML.
$output .= $indent . '- ';
// Link 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="' . esc_attr[ $item->url ] .'"' : '';
$attributes .= ' class="menu-link ' . [ $depth > 0 ? 'sub-menu-link' : 'main-menu-link' ] . '"';
// Build HTML output and pass through the proper filter.
$item_output = sprintf[ '%1$s%3$s%4$s%5$s%6$s',
$args->before,
$attributes,
$args->link_before,
apply_filters[ 'the_title', $item->title, $item->ID ],
$args->link_after,
$args->after
];
$output .= apply_filters[ 'walker_nav_menu_start_el', $item_output, $item, $depth, $args ];
}
}
0 trên mục danh sách bên phải khi sử dụng trình tạo menu mặc định]Tháng Tư 25, 2017 lúc 5. 57 giờ sáng
Beverleyh
người tham gia
@Atelierbram cảm ơn. . D
Đúng, cũng có nhiều cách để làm điều đó trong PHP. Đối với bất kỳ ai không sử dụng khung có thêm lớp, đây là một cách đơn giản để thực hiện điều đó trong PHP…
Ở đầu trang web – trên tất cả HTML, thậm chí cả doctype;
Điều này sẽ lấy giá trị “about-us” từ một URL giống như http. //www. trang web của tôi. com/about-us. php
Layload là một kỹ thuật JS để đảm bảo tải hình ảnh khi chúng xuất hiện trong chế độ xem để giảm thời gian tải trang của bạn. Trên hết, các ngón tay cái từ kirby có thể giúp bạn thay đổi kích thước hình ảnh của mình theo chiều rộng của chế độ xem
Nếu bạn đã thêm phần này vào tệp cấu hình của mình chẳng hạn
'thumbs' => [
'srcsets' => [
'teaser' => [
'800w' => ['width' => 800, 'quality' => 100],
'1024w' => ['width' => 1024, 'quality' => 100],
'1440w' => ['width' => 1440, 'quality' => 100],
'2048w' => ['width' => 2048, 'quality' => 100]
]
]
]
Sau đó, bạn có thể đặt các loại dòng này cho tất cả các hình ảnh của mình
wp_nav_menu[ array[
'menu' => 'Something custom walker',
'walker' => new WPDocs_Walker_Nav_Menu[]
] ];
/**
* Custom walker class.
*/
class WPDocs_Walker_Nav_Menu extends Walker_Nav_Menu {
/**
* Starts the list before the elements are added.
*
* Adds classes to the unordered list sub-menus.
*
* @param string $output Passed by reference. Used to append additional content.
* @param int $depth Depth of menu item. Used for padding.
* @param array $args An array of arguments. @see wp_nav_menu[]
*/
function start_lvl[ &$output, $depth = 0, $args = array[] ] {
// Depth-dependent classes.
$indent = [ $depth > 0 ? str_repeat[ "\t", $depth ] : '' ]; // code indent
$display_depth = [ $depth + 1]; // because it counts the first submenu as 0
$classes = array[
'sub-menu',
[ $display_depth % 2 ? 'menu-odd' : 'menu-even' ],
[ $display_depth >=2 ? 'sub-sub-menu' : '' ],
'menu-depth-' . $display_depth
];
$class_names = implode[ ' ', $classes ];
// Build HTML for output.
$output .= "\n" . $indent . '' . "\n";
}
/**
* Start the element output.
*
* Adds main/sub-classes to the list items and links.
*
* @param string $output Passed by reference. Used to append additional content.
* @param object $item Menu item data object.
* @param int $depth Depth of menu item. Used for padding.
* @param array $args An array of arguments. @see wp_nav_menu[]
* @param int $id Current item ID.
*/
function start_el[ &$output, $item, $depth = 0, $args = array[], $id = 0 ] {
global $wp_query;
$indent = [ $depth > 0 ? str_repeat[ "\t", $depth ] : '' ]; // code indent
// Depth-dependent classes.
$depth_classes = array[
[ $depth == 0 ? 'main-menu-item' : 'sub-menu-item' ],
[ $depth >=2 ? 'sub-sub-menu-item' : '' ],
[ $depth % 2 ? 'menu-item-odd' : 'menu-item-even' ],
'menu-item-depth-' . $depth
];
$depth_class_names = esc_attr[ implode[ ' ', $depth_classes ] ];
// Passed classes.
$classes = empty[ $item->classes ] ? array[] : [array] $item->classes;
$class_names = esc_attr[ implode[ ' ', apply_filters[ 'nav_menu_css_class', array_filter[ $classes ], $item ] ] ];
// Build HTML.
$output .= $indent . '- ';
// Link 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="' . esc_attr[ $item->url ] .'"' : '';
$attributes .= ' class="menu-link ' . [ $depth > 0 ? 'sub-menu-link' : 'main-menu-link' ] . '"';
// Build HTML output and pass through the proper filter.
$item_output = sprintf[ '%1$s%3$s%4$s%5$s%6$s',
$args->before,
$attributes,
$args->link_before,
apply_filters[ 'the_title', $item->title, $item->ID ],
$args->link_after,
$args->after
];
$output .= apply_filters[ 'walker_nav_menu_start_el', $item_output, $item, $depth, $args ];
}
}
1gọi Lazysizes chẳng hạn trong cuộc gọi JS của bạn
Làm thế nào các mục danh sách nên được bọc. Sử dụng định dạng printf[] với các trình giữ chỗ được đánh số. Mặc định là một ul có id và lớp
wp_nav_menu[ array[
'menu' => 'Something custom walker',
'walker' => new WPDocs_Walker_Nav_Menu[]
] ];
/**
* Custom walker class.
*/
class WPDocs_Walker_Nav_Menu extends Walker_Nav_Menu {
/**
* Starts the list before the elements are added.
*
* Adds classes to the unordered list sub-menus.
*
* @param string $output Passed by reference. Used to append additional content.
* @param int $depth Depth of menu item. Used for padding.
* @param array $args An array of arguments. @see wp_nav_menu[]
*/
function start_lvl[ &$output, $depth = 0, $args = array[] ] {
// Depth-dependent classes.
$indent = [ $depth > 0 ? str_repeat[ "\t", $depth ] : '' ]; // code indent
$display_depth = [ $depth + 1]; // because it counts the first submenu as 0
$classes = array[
'sub-menu',
[ $display_depth % 2 ? 'menu-odd' : 'menu-even' ],
[ $display_depth >=2 ? 'sub-sub-menu' : '' ],
'menu-depth-' . $display_depth
];
$class_names = implode[ ' ', $classes ];
// Build HTML for output.
$output .= "\n" . $indent . '' . "\n";
}
/**
* Start the element output.
*
* Adds main/sub-classes to the list items and links.
*
* @param string $output Passed by reference. Used to append additional content.
* @param object $item Menu item data object.
* @param int $depth Depth of menu item. Used for padding.
* @param array $args An array of arguments. @see wp_nav_menu[]
* @param int $id Current item ID.
*/
function start_el[ &$output, $item, $depth = 0, $args = array[], $id = 0 ] {
global $wp_query;
$indent = [ $depth > 0 ? str_repeat[ "\t", $depth ] : '' ]; // code indent
// Depth-dependent classes.
$depth_classes = array[
[ $depth == 0 ? 'main-menu-item' : 'sub-menu-item' ],
[ $depth >=2 ? 'sub-sub-menu-item' : '' ],
[ $depth % 2 ? 'menu-item-odd' : 'menu-item-even' ],
'menu-item-depth-' . $depth
];
$depth_class_names = esc_attr[ implode[ ' ', $depth_classes ] ];
// Passed classes.
$classes = empty[ $item->classes ] ? array[] : [array] $item->classes;
$class_names = esc_attr[ implode[ ' ', apply_filters[ 'nav_menu_css_class', array_filter[ $classes ], $item ] ] ];
// Build HTML.
$output .= $indent . '- ';
// Link 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="' . esc_attr[ $item->url ] .'"' : '';
$attributes .= ' class="menu-link ' . [ $depth > 0 ? 'sub-menu-link' : 'main-menu-link' ] . '"';
// Build HTML output and pass through the proper filter.
$item_output = sprintf[ '%1$s%3$s%4$s%5$s%6$s',
$args->before,
$attributes,
$args->link_before,
apply_filters[ 'the_title', $item->title, $item->ID ],
$args->link_after,
$args->after
];
$output .= apply_filters[ 'walker_nav_menu_start_el', $item_output, $item, $depth, $args ];
}
}
4 chuỗiCó duy trì khoảng trắng trong HTML của menu không.
Chấp nhận
wp_nav_menu[ array[
'menu' => 'Something custom walker',
'walker' => new WPDocs_Walker_Nav_Menu[]
] ];
/**
* Custom walker class.
*/
class WPDocs_Walker_Nav_Menu extends Walker_Nav_Menu {
/**
* Starts the list before the elements are added.
*
* Adds classes to the unordered list sub-menus.
*
* @param string $output Passed by reference. Used to append additional content.
* @param int $depth Depth of menu item. Used for padding.
* @param array $args An array of arguments. @see wp_nav_menu[]
*/
function start_lvl[ &$output, $depth = 0, $args = array[] ] {
// Depth-dependent classes.
$indent = [ $depth > 0 ? str_repeat[ "\t", $depth ] : '' ]; // code indent
$display_depth = [ $depth + 1]; // because it counts the first submenu as 0
$classes = array[
'sub-menu',
[ $display_depth % 2 ? 'menu-odd' : 'menu-even' ],
[ $display_depth >=2 ? 'sub-sub-menu' : '' ],
'menu-depth-' . $display_depth
];
$class_names = implode[ ' ', $classes ];
// Build HTML for output.
$output .= "\n" . $indent . '' . "\n";
}
/**
* Start the element output.
*
* Adds main/sub-classes to the list items and links.
*
* @param string $output Passed by reference. Used to append additional content.
* @param object $item Menu item data object.
* @param int $depth Depth of menu item. Used for padding.
* @param array $args An array of arguments. @see wp_nav_menu[]
* @param int $id Current item ID.
*/
function start_el[ &$output, $item, $depth = 0, $args = array[], $id = 0 ] {
global $wp_query;
$indent = [ $depth > 0 ? str_repeat[ "\t", $depth ] : '' ]; // code indent
// Depth-dependent classes.
$depth_classes = array[
[ $depth == 0 ? 'main-menu-item' : 'sub-menu-item' ],
[ $depth >=2 ? 'sub-sub-menu-item' : '' ],
[ $depth % 2 ? 'menu-item-odd' : 'menu-item-even' ],
'menu-item-depth-' . $depth
];
$depth_class_names = esc_attr[ implode[ ' ', $depth_classes ] ];
// Passed classes.
$classes = empty[ $item->classes ] ? array[] : [array] $item->classes;
$class_names = esc_attr[ implode[ ' ', apply_filters[ 'nav_menu_css_class', array_filter[ $classes ], $item ] ] ];
// Build HTML.
$output .= $indent . '- ';
// Link 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="' . esc_attr[ $item->url ] .'"' : '';
$attributes .= ' class="menu-link ' . [ $depth > 0 ? 'sub-menu-link' : 'main-menu-link' ] . '"';
// Build HTML output and pass through the proper filter.
$item_output = sprintf[ '%1$s%3$s%4$s%5$s%6$s',
$args->before,
$attributes,
$args->link_before,
apply_filters[ 'the_title', $item->title, $item->ID ],
$args->link_after,
$args->after
];
$output .= apply_filters[ 'walker_nav_menu_start_el', $item_output, $item, $depth, $args ];
}
}
5 hoặc wp_nav_menu[ array[
'menu' => 'Something custom walker',
'walker' => new WPDocs_Walker_Nav_Menu[]
] ];
/**
* Custom walker class.
*/
class WPDocs_Walker_Nav_Menu extends Walker_Nav_Menu {
/**
* Starts the list before the elements are added.
*
* Adds classes to the unordered list sub-menus.
*
* @param string $output Passed by reference. Used to append additional content.
* @param int $depth Depth of menu item. Used for padding.
* @param array $args An array of arguments. @see wp_nav_menu[]
*/
function start_lvl[ &$output, $depth = 0, $args = array[] ] {
// Depth-dependent classes.
$indent = [ $depth > 0 ? str_repeat[ "\t", $depth ] : '' ]; // code indent
$display_depth = [ $depth + 1]; // because it counts the first submenu as 0
$classes = array[
'sub-menu',
[ $display_depth % 2 ? 'menu-odd' : 'menu-even' ],
[ $display_depth >=2 ? 'sub-sub-menu' : '' ],
'menu-depth-' . $display_depth
];
$class_names = implode[ ' ', $classes ];
// Build HTML for output.
$output .= "\n" . $indent . '' . "\n";
}
/**
* Start the element output.
*
* Adds main/sub-classes to the list items and links.
*
* @param string $output Passed by reference. Used to append additional content.
* @param object $item Menu item data object.
* @param int $depth Depth of menu item. Used for padding.
* @param array $args An array of arguments. @see wp_nav_menu[]
* @param int $id Current item ID.
*/
function start_el[ &$output, $item, $depth = 0, $args = array[], $id = 0 ] {
global $wp_query;
$indent = [ $depth > 0 ? str_repeat[ "\t", $depth ] : '' ]; // code indent
// Depth-dependent classes.
$depth_classes = array[
[ $depth == 0 ? 'main-menu-item' : 'sub-menu-item' ],
[ $depth >=2 ? 'sub-sub-menu-item' : '' ],
[ $depth % 2 ? 'menu-item-odd' : 'menu-item-even' ],
'menu-item-depth-' . $depth
];
$depth_class_names = esc_attr[ implode[ ' ', $depth_classes ] ];
// Passed classes.
$classes = empty[ $item->classes ] ? array[] : [array] $item->classes;
$class_names = esc_attr[ implode[ ' ', apply_filters[ 'nav_menu_css_class', array_filter[ $classes ], $item ] ] ];
// Build HTML.
$output .= $indent . '- ';
// Link 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="' . esc_attr[ $item->url ] .'"' : '';
$attributes .= ' class="menu-link ' . [ $depth > 0 ? 'sub-menu-link' : 'main-menu-link' ] . '"';
// Build HTML output and pass through the proper filter.
$item_output = sprintf[ '%1$s%3$s%4$s%5$s%6$s',
$args->before,
$attributes,
$args->link_before,
apply_filters[ 'the_title', $item->title, $item->ID ],
$args->link_after,
$args->after
];
$output .= apply_filters[ 'walker_nav_menu_start_el', $item_output, $item, $depth, $args ];
}
}
6. Mặc định wp_nav_menu[ array[
'menu' => 'Something custom walker',
'walker' => new WPDocs_Walker_Nav_Menu[]
] ];
/**
* Custom walker class.
*/
class WPDocs_Walker_Nav_Menu extends Walker_Nav_Menu {
/**
* Starts the list before the elements are added.
*
* Adds classes to the unordered list sub-menus.
*
* @param string $output Passed by reference. Used to append additional content.
* @param int $depth Depth of menu item. Used for padding.
* @param array $args An array of arguments. @see wp_nav_menu[]
*/
function start_lvl[ &$output, $depth = 0, $args = array[] ] {
// Depth-dependent classes.
$indent = [ $depth > 0 ? str_repeat[ "\t", $depth ] : '' ]; // code indent
$display_depth = [ $depth + 1]; // because it counts the first submenu as 0
$classes = array[
'sub-menu',
[ $display_depth % 2 ? 'menu-odd' : 'menu-even' ],
[ $display_depth >=2 ? 'sub-sub-menu' : '' ],
'menu-depth-' . $display_depth
];
$class_names = implode[ ' ', $classes ];
// Build HTML for output.
$output .= "\n" . $indent . '' . "\n";
}
/**
* Start the element output.
*
* Adds main/sub-classes to the list items and links.
*
* @param string $output Passed by reference. Used to append additional content.
* @param object $item Menu item data object.
* @param int $depth Depth of menu item. Used for padding.
* @param array $args An array of arguments. @see wp_nav_menu[]
* @param int $id Current item ID.
*/
function start_el[ &$output, $item, $depth = 0, $args = array[], $id = 0 ] {
global $wp_query;
$indent = [ $depth > 0 ? str_repeat[ "\t", $depth ] : '' ]; // code indent
// Depth-dependent classes.
$depth_classes = array[
[ $depth == 0 ? 'main-menu-item' : 'sub-menu-item' ],
[ $depth >=2 ? 'sub-sub-menu-item' : '' ],
[ $depth % 2 ? 'menu-item-odd' : 'menu-item-even' ],
'menu-item-depth-' . $depth
];
$depth_class_names = esc_attr[ implode[ ' ', $depth_classes ] ];
// Passed classes.
$classes = empty[ $item->classes ] ? array[] : [array] $item->classes;
$class_names = esc_attr[ implode[ ' ', apply_filters[ 'nav_menu_css_class', array_filter[ $classes ], $item ] ] ];
// Build HTML.
$output .= $indent . '- ';
// Link 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="' . esc_attr[ $item->url ] .'"' : '';
$attributes .= ' class="menu-link ' . [ $depth > 0 ? 'sub-menu-link' : 'main-menu-link' ] . '"';
// Build HTML output and pass through the proper filter.
$item_output = sprintf[ '%1$s%3$s%4$s%5$s%6$s',
$args->before,
$attributes,
$args->link_before,
apply_filters[ 'the_title', $item->title, $item->ID ],
$args->link_after,
$args->after
];
$output .= apply_filters[ 'walker_nav_menu_start_el', $item_output, $item, $depth, $args ];
}
}
5. Mặc định.
wp_nav_menu[ array[
'menu' => 'Something custom walker',
'walker' => new WPDocs_Walker_Nav_Menu[]
] ];
/**
* Custom walker class.
*/
class WPDocs_Walker_Nav_Menu extends Walker_Nav_Menu {
/**
* Starts the list before the elements are added.
*
* Adds classes to the unordered list sub-menus.
*
* @param string $output Passed by reference. Used to append additional content.
* @param int $depth Depth of menu item. Used for padding.
* @param array $args An array of arguments. @see wp_nav_menu[]
*/
function start_lvl[ &$output, $depth = 0, $args = array[] ] {
// Depth-dependent classes.
$indent = [ $depth > 0 ? str_repeat[ "\t", $depth ] : '' ]; // code indent
$display_depth = [ $depth + 1]; // because it counts the first submenu as 0
$classes = array[
'sub-menu',
[ $display_depth % 2 ? 'menu-odd' : 'menu-even' ],
[ $display_depth >=2 ? 'sub-sub-menu' : '' ],
'menu-depth-' . $display_depth
];
$class_names = implode[ ' ', $classes ];
// Build HTML for output.
$output .= "\n" . $indent . '' . "\n";
}
/**
* Start the element output.
*
* Adds main/sub-classes to the list items and links.
*
* @param string $output Passed by reference. Used to append additional content.
* @param object $item Menu item data object.
* @param int $depth Depth of menu item. Used for padding.
* @param array $args An array of arguments. @see wp_nav_menu[]
* @param int $id Current item ID.
*/
function start_el[ &$output, $item, $depth = 0, $args = array[], $id = 0 ] {
global $wp_query;
$indent = [ $depth > 0 ? str_repeat[ "\t", $depth ] : '' ]; // code indent
// Depth-dependent classes.
$depth_classes = array[
[ $depth == 0 ? 'main-menu-item' : 'sub-menu-item' ],
[ $depth >=2 ? 'sub-sub-menu-item' : '' ],
[ $depth % 2 ? 'menu-item-odd' : 'menu-item-even' ],
'menu-item-depth-' . $depth
];
$depth_class_names = esc_attr[ implode[ ' ', $depth_classes ] ];
// Passed classes.
$classes = empty[ $item->classes ] ? array[] : [array] $item->classes;
$class_names = esc_attr[ implode[ ' ', apply_filters[ 'nav_menu_css_class', array_filter[ $classes ], $item ] ] ];
// Build HTML.
$output .= $indent . '- ';
// Link 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="' . esc_attr[ $item->url ] .'"' : '';
$attributes .= ' class="menu-link ' . [ $depth > 0 ? 'sub-menu-link' : 'main-menu-link' ] . '"';
// Build HTML output and pass through the proper filter.
$item_output = sprintf[ '%1$s%3$s%4$s%5$s%6$s',
$args->before,
$attributes,
$args->link_before,
apply_filters[ 'the_title', $item->title, $item->ID ],
$args->link_after,
$args->after
];
$output .= apply_filters[ 'walker_nav_menu_start_el', $item_output, $item, $depth, $args ];
}
}
8khoảng trống. chuỗi. false Vô hiệu nếu đối số
wp_nav_menu[ array[
'menu' => 'Something custom walker',
'walker' => new WPDocs_Walker_Nav_Menu[]
] ];
/**
* Custom walker class.
*/
class WPDocs_Walker_Nav_Menu extends Walker_Nav_Menu {
/**
* Starts the list before the elements are added.
*
* Adds classes to the unordered list sub-menus.
*
* @param string $output Passed by reference. Used to append additional content.
* @param int $depth Depth of menu item. Used for padding.
* @param array $args An array of arguments. @see wp_nav_menu[]
*/
function start_lvl[ &$output, $depth = 0, $args = array[] ] {
// Depth-dependent classes.
$indent = [ $depth > 0 ? str_repeat[ "\t", $depth ] : '' ]; // code indent
$display_depth = [ $depth + 1]; // because it counts the first submenu as 0
$classes = array[
'sub-menu',
[ $display_depth % 2 ? 'menu-odd' : 'menu-even' ],
[ $display_depth >=2 ? 'sub-sub-menu' : '' ],
'menu-depth-' . $display_depth
];
$class_names = implode[ ' ', $classes ];
// Build HTML for output.
$output .= "\n" . $indent . '' . "\n";
}
/**
* Start the element output.
*
* Adds main/sub-classes to the list items and links.
*
* @param string $output Passed by reference. Used to append additional content.
* @param object $item Menu item data object.
* @param int $depth Depth of menu item. Used for padding.
* @param array $args An array of arguments. @see wp_nav_menu[]
* @param int $id Current item ID.
*/
function start_el[ &$output, $item, $depth = 0, $args = array[], $id = 0 ] {
global $wp_query;
$indent = [ $depth > 0 ? str_repeat[ "\t", $depth ] : '' ]; // code indent
// Depth-dependent classes.
$depth_classes = array[
[ $depth == 0 ? 'main-menu-item' : 'sub-menu-item' ],
[ $depth >=2 ? 'sub-sub-menu-item' : '' ],
[ $depth % 2 ? 'menu-item-odd' : 'menu-item-even' ],
'menu-item-depth-' . $depth
];
$depth_class_names = esc_attr[ implode[ ' ', $depth_classes ] ];
// Passed classes.
$classes = empty[ $item->classes ] ? array[] : [array] $item->classes;
$class_names = esc_attr[ implode[ ' ', apply_filters[ 'nav_menu_css_class', array_filter[ $classes ], $item ] ] ];
// Build HTML.
$output .= $indent . '- ';
// Link 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="' . esc_attr[ $item->url ] .'"' : '';
$attributes .= ' class="menu-link ' . [ $depth > 0 ? 'sub-menu-link' : 'main-menu-link' ] . '"';
// Build HTML output and pass through the proper filter.
$item_output = sprintf[ '%1$s%3$s%4$s%5$s%6$s',
$args->before,
$attributes,
$args->link_before,
apply_filters[ 'the_title', $item->title, $item->ID ],
$args->link_after,
$args->after
];
$output .= apply_filters[ 'walker_nav_menu_start_el', $item_output, $item, $depth, $args ];
}
}
9 là đúng, xuất menu nếu wp_nav_menu[ array[
'menu' => 'Something custom walker',
'walker' => new WPDocs_Walker_Nav_Menu[]
] ];
/**
* Custom walker class.
*/
class WPDocs_Walker_Nav_Menu extends Walker_Nav_Menu {
/**
* Starts the list before the elements are added.
*
* Adds classes to the unordered list sub-menus.
*
* @param string $output Passed by reference. Used to append additional content.
* @param int $depth Depth of menu item. Used for padding.
* @param array $args An array of arguments. @see wp_nav_menu[]
*/
function start_lvl[ &$output, $depth = 0, $args = array[] ] {
// Depth-dependent classes.
$indent = [ $depth > 0 ? str_repeat[ "\t", $depth ] : '' ]; // code indent
$display_depth = [ $depth + 1]; // because it counts the first submenu as 0
$classes = array[
'sub-menu',
[ $display_depth % 2 ? 'menu-odd' : 'menu-even' ],
[ $display_depth >=2 ? 'sub-sub-menu' : '' ],
'menu-depth-' . $display_depth
];
$class_names = implode[ ' ', $classes ];
// Build HTML for output.
$output .= "\n" . $indent . '' . "\n";
}
/**
* Start the element output.
*
* Adds main/sub-classes to the list items and links.
*
* @param string $output Passed by reference. Used to append additional content.
* @param object $item Menu item data object.
* @param int $depth Depth of menu item. Used for padding.
* @param array $args An array of arguments. @see wp_nav_menu[]
* @param int $id Current item ID.
*/
function start_el[ &$output, $item, $depth = 0, $args = array[], $id = 0 ] {
global $wp_query;
$indent = [ $depth > 0 ? str_repeat[ "\t", $depth ] : '' ]; // code indent
// Depth-dependent classes.
$depth_classes = array[
[ $depth == 0 ? 'main-menu-item' : 'sub-menu-item' ],
[ $depth >=2 ? 'sub-sub-menu-item' : '' ],
[ $depth % 2 ? 'menu-item-odd' : 'menu-item-even' ],
'menu-item-depth-' . $depth
];
$depth_class_names = esc_attr[ implode[ ' ', $depth_classes ] ];
// Passed classes.
$classes = empty[ $item->classes ] ? array[] : [array] $item->classes;
$class_names = esc_attr[ implode[ ' ', apply_filters[ 'nav_menu_css_class', array_filter[ $classes ], $item ] ] ];
// Build HTML.
$output .= $indent . '- ';
// Link 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="' . esc_attr[ $item->url ] .'"' : '';
$attributes .= ' class="menu-link ' . [ $depth > 0 ? 'sub-menu-link' : 'main-menu-link' ] . '"';
// Build HTML output and pass through the proper filter.
$item_output = sprintf[ '%1$s%3$s%4$s%5$s%6$s',
$args->before,
$attributes,
$args->link_before,
apply_filters[ 'the_title', $item->title, $item->ID ],
$args->link_after,
$args->after
];
$output .= apply_filters[ 'walker_nav_menu_start_el', $item_output, $item, $depth, $args ];
}
}
9 là sai. Sai nếu không có mục nào hoặc không tìm thấy menu nào.
'thumbs' => [
'srcsets' => [
'teaser' => [
'800w' => ['width' => 800, 'quality' => 100],
'1024w' => ['width' => 1024, 'quality' => 100],
'1440w' => ['width' => 1440, 'quality' => 100],
'2048w' => ['width' => 2048, 'quality' => 100]
]
]
]
0Đưa ra một tham số theme_location, hàm sẽ hiển thị menu được gán cho vị trí đó. Nếu không có vị trí nào như vậy tồn tại hoặc không có menu nào được gán cho vị trí đó, tham số fallback_cb sẽ xác định nội dung được hiển thị
Nếu không được cung cấp tham số theme_location, hàm sẽ hiển thị
- menu khớp với ID, slug hoặc tên do thông số menu cung cấp;
- mặt khác, menu không trống đầu tiên;
- mặt khác [hoặc nếu menu được cung cấp bởi menu trống], đầu ra của hàm được cung cấp bởi tham số fallback_cb [wp_page_menu[] , theo mặc định];
- nếu không thì không có gì
The following classes are applied to menu items, i.e. to the HTML
'thumbs' => [
'srcsets' => [
'teaser' => [
'800w' => ['width' => 800, 'quality' => 100],
'1024w' => ['width' => 1024, 'quality' => 100],
'1440w' => ['width' => 1440, 'quality' => 100],
'2048w' => ['width' => 2048, 'quality' => 100]
]
]
]
11:
12'thumbs' => [ 'srcsets' => [ 'teaser' => [ '800w' => ['width' => 800, 'quality' => 100], '1024w' => ['width' => 1024, 'quality' => 100], '1440w' => ['width' => 1440, 'quality' => 100], '2048w' => ['width' => 2048, 'quality' => 100] ] ] ]
Lớp này được thêm vào mọi mục menu.
13'thumbs' => [ 'srcsets' => [ 'teaser' => [ '800w' => ['width' => 800, 'quality' => 100], '1024w' => ['width' => 1024, 'quality' => 100], '1440w' => ['width' => 1440, 'quality' => 100], '2048w' => ['width' => 2048, 'quality' => 100] ] ] ]
Lớp này được thêm vào mục menu có mục phụ.
14'thumbs' => [ 'srcsets' => [ 'teaser' => [ '800w' => ['width' => 800, 'quality' => 100], '1024w' => ['width' => 1024, 'quality' => 100], '1440w' => ['width' => 1440, 'quality' => 100], '2048w' => ['width' => 2048, 'quality' => 100] ] ] ]
Lớp này được thêm vào mọi mục menu, trong đó {object} là loại bài đăng hoặc phân loại.
15'thumbs' => [ 'srcsets' => [ 'teaser' => [ '800w' => ['width' => 800, 'quality' => 100], '1024w' => ['width' => 1024, 'quality' => 100], '1440w' => ['width' => 1440, 'quality' => 100], '2048w' => ['width' => 2048, 'quality' => 100] ] ] ]
Lớp này được thêm vào các mục menu tương ứng với một danh mục.
16'thumbs' => [ 'srcsets' => [ 'teaser' => [ '800w' => ['width' => 800, 'quality' => 100], '1024w' => ['width' => 1024, 'quality' => 100], '1440w' => ['width' => 1440, 'quality' => 100], '2048w' => ['width' => 2048, 'quality' => 100] ] ] ]
Lớp này được thêm vào các mục menu tương ứng với thẻ.
17'thumbs' => [ 'srcsets' => [ 'teaser' => [ '800w' => ['width' => 800, 'quality' => 100], '1024w' => ['width' => 1024, 'quality' => 100], '1440w' => ['width' => 1440, 'quality' => 100], '2048w' => ['width' => 2048, 'quality' => 100] ] ] ]
Lớp này được thêm vào các mục menu tương ứng với các trang tĩnh.
18'thumbs' => [ 'srcsets' => [ 'teaser' => [ '800w' => ['width' => 800, 'quality' => 100], '1024w' => ['width' => 1024, 'quality' => 100], '1440w' => ['width' => 1440, 'quality' => 100], '2048w' => ['width' => 2048, 'quality' => 100] ] ] ]
Lớp này được thêm vào các mục menu tương ứng với loại bài đăng tùy chỉnh hoặc phân loại tùy chỉnh.
19'thumbs' => [ 'srcsets' => [ 'teaser' => [ '800w' => ['width' => 800, 'quality' => 100], '1024w' => ['width' => 1024, 'quality' => 100], '1440w' => ['width' => 1440, 'quality' => 100], '2048w' => ['width' => 2048, 'quality' => 100] ] ] ]
Lớp này được thêm vào mọi mục menu, trong đó {type} là “post_type” hoặc “taxonomy”.
00wp_nav_menu[ array[ 'menu' => 'Something custom walker', 'walker' => new WPDocs_Walker_Nav_Menu[] ] ]; /** * Custom walker class. */ class WPDocs_Walker_Nav_Menu extends Walker_Nav_Menu { /** * Starts the list before the elements are added. * * Adds classes to the unordered list sub-menus. * * @param string $output Passed by reference. Used to append additional content. * @param int $depth Depth of menu item. Used for padding. * @param array $args An array of arguments. @see wp_nav_menu[] */ function start_lvl[ &$output, $depth = 0, $args = array[] ] { // Depth-dependent classes. $indent = [ $depth > 0 ? str_repeat[ "\t", $depth ] : '' ]; // code indent $display_depth = [ $depth + 1]; // because it counts the first submenu as 0 $classes = array[ 'sub-menu', [ $display_depth % 2 ? 'menu-odd' : 'menu-even' ], [ $display_depth >=2 ? 'sub-sub-menu' : '' ], 'menu-depth-' . $display_depth ]; $class_names = implode[ ' ', $classes ]; // Build HTML for output. $output .= "\n" . $indent . '
- ' . "\n";
}
/**
* Start the element output.
*
* Adds main/sub-classes to the list items and links.
*
* @param string $output Passed by reference. Used to append additional content.
* @param object $item Menu item data object.
* @param int $depth Depth of menu item. Used for padding.
* @param array $args An array of arguments. @see wp_nav_menu[]
* @param int $id Current item ID.
*/
function start_el[ &$output, $item, $depth = 0, $args = array[], $id = 0 ] {
global $wp_query;
$indent = [ $depth > 0 ? str_repeat[ "\t", $depth ] : '' ]; // code indent
// Depth-dependent classes.
$depth_classes = array[
[ $depth == 0 ? 'main-menu-item' : 'sub-menu-item' ],
[ $depth >=2 ? 'sub-sub-menu-item' : '' ],
[ $depth % 2 ? 'menu-item-odd' : 'menu-item-even' ],
'menu-item-depth-' . $depth
];
$depth_class_names = esc_attr[ implode[ ' ', $depth_classes ] ];
// Passed classes.
$classes = empty[ $item->classes ] ? array[] : [array] $item->classes;
$class_names = esc_attr[ implode[ ' ', apply_filters[ 'nav_menu_css_class', array_filter[ $classes ], $item ] ] ];
// Build HTML.
$output .= $indent . '
- '; // Link 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="' . esc_attr[ $item->url ] .'"' : ''; $attributes .= ' class="menu-link ' . [ $depth > 0 ? 'sub-menu-link' : 'main-menu-link' ] . '"'; // Build HTML output and pass through the proper filter. $item_output = sprintf[ '%1$s%3$s%4$s%5$s%6$s', $args->before, $attributes, $args->link_before, apply_filters[ 'the_title', $item->title, $item->ID ], $args->link_after, $args->after ]; $output .= apply_filters[ 'walker_nav_menu_start_el', $item_output, $item, $depth, $args ]; } }
Lớp này được thêm vào các mục menu tương ứng với các loại bài đăng. tôi. e. trang tĩnh hoặc loại bài đăng tùy chỉnh.
01wp_nav_menu[ array[ 'menu' => 'Something custom walker', 'walker' => new WPDocs_Walker_Nav_Menu[] ] ]; /** * Custom walker class. */ class WPDocs_Walker_Nav_Menu extends Walker_Nav_Menu { /** * Starts the list before the elements are added. * * Adds classes to the unordered list sub-menus. * * @param string $output Passed by reference. Used to append additional content. * @param int $depth Depth of menu item. Used for padding. * @param array $args An array of arguments. @see wp_nav_menu[] */ function start_lvl[ &$output, $depth = 0, $args = array[] ] { // Depth-dependent classes. $indent = [ $depth > 0 ? str_repeat[ "\t", $depth ] : '' ]; // code indent $display_depth = [ $depth + 1]; // because it counts the first submenu as 0 $classes = array[ 'sub-menu', [ $display_depth % 2 ? 'menu-odd' : 'menu-even' ], [ $display_depth >=2 ? 'sub-sub-menu' : '' ], 'menu-depth-' . $display_depth ]; $class_names = implode[ ' ', $classes ]; // Build HTML for output. $output .= "\n" . $indent . '
- ' . "\n";
}
/**
* Start the element output.
*
* Adds main/sub-classes to the list items and links.
*
* @param string $output Passed by reference. Used to append additional content.
* @param object $item Menu item data object.
* @param int $depth Depth of menu item. Used for padding.
* @param array $args An array of arguments. @see wp_nav_menu[]
* @param int $id Current item ID.
*/
function start_el[ &$output, $item, $depth = 0, $args = array[], $id = 0 ] {
global $wp_query;
$indent = [ $depth > 0 ? str_repeat[ "\t", $depth ] : '' ]; // code indent
// Depth-dependent classes.
$depth_classes = array[
[ $depth == 0 ? 'main-menu-item' : 'sub-menu-item' ],
[ $depth >=2 ? 'sub-sub-menu-item' : '' ],
[ $depth % 2 ? 'menu-item-odd' : 'menu-item-even' ],
'menu-item-depth-' . $depth
];
$depth_class_names = esc_attr[ implode[ ' ', $depth_classes ] ];
// Passed classes.
$classes = empty[ $item->classes ] ? array[] : [array] $item->classes;
$class_names = esc_attr[ implode[ ' ', apply_filters[ 'nav_menu_css_class', array_filter[ $classes ], $item ] ] ];
// Build HTML.
$output .= $indent . '
- '; // Link 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="' . esc_attr[ $item->url ] .'"' : ''; $attributes .= ' class="menu-link ' . [ $depth > 0 ? 'sub-menu-link' : 'main-menu-link' ] . '"'; // Build HTML output and pass through the proper filter. $item_output = sprintf[ '%1$s%3$s%4$s%5$s%6$s', $args->before, $attributes, $args->link_before, apply_filters[ 'the_title', $item->title, $item->ID ], $args->link_after, $args->after ]; $output .= apply_filters[ 'walker_nav_menu_start_el', $item_output, $item, $depth, $args ]; } }
Lớp này được thêm vào các mục menu tương ứng với các nguyên tắc phân loại. tôi. e. danh mục, thẻ hoặc phân loại tùy chỉnh.
02wp_nav_menu[ array[ 'menu' => 'Something custom walker', 'walker' => new WPDocs_Walker_Nav_Menu[] ] ]; /** * Custom walker class. */ class WPDocs_Walker_Nav_Menu extends Walker_Nav_Menu { /** * Starts the list before the elements are added. * * Adds classes to the unordered list sub-menus. * * @param string $output Passed by reference. Used to append additional content. * @param int $depth Depth of menu item. Used for padding. * @param array $args An array of arguments. @see wp_nav_menu[] */ function start_lvl[ &$output, $depth = 0, $args = array[] ] { // Depth-dependent classes. $indent = [ $depth > 0 ? str_repeat[ "\t", $depth ] : '' ]; // code indent $display_depth = [ $depth + 1]; // because it counts the first submenu as 0 $classes = array[ 'sub-menu', [ $display_depth % 2 ? 'menu-odd' : 'menu-even' ], [ $display_depth >=2 ? 'sub-sub-menu' : '' ], 'menu-depth-' . $display_depth ]; $class_names = implode[ ' ', $classes ]; // Build HTML for output. $output .= "\n" . $indent . '
- ' . "\n";
}
/**
* Start the element output.
*
* Adds main/sub-classes to the list items and links.
*
* @param string $output Passed by reference. Used to append additional content.
* @param object $item Menu item data object.
* @param int $depth Depth of menu item. Used for padding.
* @param array $args An array of arguments. @see wp_nav_menu[]
* @param int $id Current item ID.
*/
function start_el[ &$output, $item, $depth = 0, $args = array[], $id = 0 ] {
global $wp_query;
$indent = [ $depth > 0 ? str_repeat[ "\t", $depth ] : '' ]; // code indent
// Depth-dependent classes.
$depth_classes = array[
[ $depth == 0 ? 'main-menu-item' : 'sub-menu-item' ],
[ $depth >=2 ? 'sub-sub-menu-item' : '' ],
[ $depth % 2 ? 'menu-item-odd' : 'menu-item-even' ],
'menu-item-depth-' . $depth
];
$depth_class_names = esc_attr[ implode[ ' ', $depth_classes ] ];
// Passed classes.
$classes = empty[ $item->classes ] ? array[] : [array] $item->classes;
$class_names = esc_attr[ implode[ ' ', apply_filters[ 'nav_menu_css_class', array_filter[ $classes ], $item ] ] ];
// Build HTML.
$output .= $indent . '
- '; // Link 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="' . esc_attr[ $item->url ] .'"' : ''; $attributes .= ' class="menu-link ' . [ $depth > 0 ? 'sub-menu-link' : 'main-menu-link' ] . '"'; // Build HTML output and pass through the proper filter. $item_output = sprintf[ '%1$s%3$s%4$s%5$s%6$s', $args->before, $attributes, $args->link_before, apply_filters[ 'the_title', $item->title, $item->ID ], $args->link_after, $args->after ]; $output .= apply_filters[ 'walker_nav_menu_start_el', $item_output, $item, $depth, $args ]; } }
Lớp này được thêm vào các mục menu tương ứng với trang hiện được hiển thị.
03wp_nav_menu[ array[ 'menu' => 'Something custom walker', 'walker' => new WPDocs_Walker_Nav_Menu[] ] ]; /** * Custom walker class. */ class WPDocs_Walker_Nav_Menu extends Walker_Nav_Menu { /** * Starts the list before the elements are added. * * Adds classes to the unordered list sub-menus. * * @param string $output Passed by reference. Used to append additional content. * @param int $depth Depth of menu item. Used for padding. * @param array $args An array of arguments. @see wp_nav_menu[] */ function start_lvl[ &$output, $depth = 0, $args = array[] ] { // Depth-dependent classes. $indent = [ $depth > 0 ? str_repeat[ "\t", $depth ] : '' ]; // code indent $display_depth = [ $depth + 1]; // because it counts the first submenu as 0 $classes = array[ 'sub-menu', [ $display_depth % 2 ? 'menu-odd' : 'menu-even' ], [ $display_depth >=2 ? 'sub-sub-menu' : '' ], 'menu-depth-' . $display_depth ]; $class_names = implode[ ' ', $classes ]; // Build HTML for output. $output .= "\n" . $indent . '
- ' . "\n";
}
/**
* Start the element output.
*
* Adds main/sub-classes to the list items and links.
*
* @param string $output Passed by reference. Used to append additional content.
* @param object $item Menu item data object.
* @param int $depth Depth of menu item. Used for padding.
* @param array $args An array of arguments. @see wp_nav_menu[]
* @param int $id Current item ID.
*/
function start_el[ &$output, $item, $depth = 0, $args = array[], $id = 0 ] {
global $wp_query;
$indent = [ $depth > 0 ? str_repeat[ "\t", $depth ] : '' ]; // code indent
// Depth-dependent classes.
$depth_classes = array[
[ $depth == 0 ? 'main-menu-item' : 'sub-menu-item' ],
[ $depth >=2 ? 'sub-sub-menu-item' : '' ],
[ $depth % 2 ? 'menu-item-odd' : 'menu-item-even' ],
'menu-item-depth-' . $depth
];
$depth_class_names = esc_attr[ implode[ ' ', $depth_classes ] ];
// Passed classes.
$classes = empty[ $item->classes ] ? array[] : [array] $item->classes;
$class_names = esc_attr[ implode[ ' ', apply_filters[ 'nav_menu_css_class', array_filter[ $classes ], $item ] ] ];
// Build HTML.
$output .= $indent . '
- '; // Link 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="' . esc_attr[ $item->url ] .'"' : ''; $attributes .= ' class="menu-link ' . [ $depth > 0 ? 'sub-menu-link' : 'main-menu-link' ] . '"'; // Build HTML output and pass through the proper filter. $item_output = sprintf[ '%1$s%3$s%4$s%5$s%6$s', $args->before, $attributes, $args->link_before, apply_filters[ 'the_title', $item->title, $item->ID ], $args->link_after, $args->after ]; $output .= apply_filters[ 'walker_nav_menu_start_el', $item_output, $item, $depth, $args ]; } }
Lớp này được thêm vào các mục menu tương ứng với cấp bậc gốc của trang hiện được hiển thị.
04wp_nav_menu[ array[ 'menu' => 'Something custom walker', 'walker' => new WPDocs_Walker_Nav_Menu[] ] ]; /** * Custom walker class. */ class WPDocs_Walker_Nav_Menu extends Walker_Nav_Menu { /** * Starts the list before the elements are added. * * Adds classes to the unordered list sub-menus. * * @param string $output Passed by reference. Used to append additional content. * @param int $depth Depth of menu item. Used for padding. * @param array $args An array of arguments. @see wp_nav_menu[] */ function start_lvl[ &$output, $depth = 0, $args = array[] ] { // Depth-dependent classes. $indent = [ $depth > 0 ? str_repeat[ "\t", $depth ] : '' ]; // code indent $display_depth = [ $depth + 1]; // because it counts the first submenu as 0 $classes = array[ 'sub-menu', [ $display_depth % 2 ? 'menu-odd' : 'menu-even' ], [ $display_depth >=2 ? 'sub-sub-menu' : '' ], 'menu-depth-' . $display_depth ]; $class_names = implode[ ' ', $classes ]; // Build HTML for output. $output .= "\n" . $indent . '
- ' . "\n";
}
/**
* Start the element output.
*
* Adds main/sub-classes to the list items and links.
*
* @param string $output Passed by reference. Used to append additional content.
* @param object $item Menu item data object.
* @param int $depth Depth of menu item. Used for padding.
* @param array $args An array of arguments. @see wp_nav_menu[]
* @param int $id Current item ID.
*/
function start_el[ &$output, $item, $depth = 0, $args = array[], $id = 0 ] {
global $wp_query;
$indent = [ $depth > 0 ? str_repeat[ "\t", $depth ] : '' ]; // code indent
// Depth-dependent classes.
$depth_classes = array[
[ $depth == 0 ? 'main-menu-item' : 'sub-menu-item' ],
[ $depth >=2 ? 'sub-sub-menu-item' : '' ],
[ $depth % 2 ? 'menu-item-odd' : 'menu-item-even' ],
'menu-item-depth-' . $depth
];
$depth_class_names = esc_attr[ implode[ ' ', $depth_classes ] ];
// Passed classes.
$classes = empty[ $item->classes ] ? array[] : [array] $item->classes;
$class_names = esc_attr[ implode[ ' ', apply_filters[ 'nav_menu_css_class', array_filter[ $classes ], $item ] ] ];
// Build HTML.
$output .= $indent . '
- '; // Link 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="' . esc_attr[ $item->url ] .'"' : ''; $attributes .= ' class="menu-link ' . [ $depth > 0 ? 'sub-menu-link' : 'main-menu-link' ] . '"'; // Build HTML output and pass through the proper filter. $item_output = sprintf[ '%1$s%3$s%4$s%5$s%6$s', $args->before, $attributes, $args->link_before, apply_filters[ 'the_title', $item->title, $item->ID ], $args->link_after, $args->after ]; $output .= apply_filters[ 'walker_nav_menu_start_el', $item_output, $item, $depth, $args ]; } }
Lớp này được thêm vào các mục menu tương ứng với cấp bậc gốc của đối tượng hiện được hiển thị, trong đó {object} tương ứng với giá trị được sử dụng cho. menu-item-object-{object}.
05wp_nav_menu[ array[ 'menu' => 'Something custom walker', 'walker' => new WPDocs_Walker_Nav_Menu[] ] ]; /** * Custom walker class. */ class WPDocs_Walker_Nav_Menu extends Walker_Nav_Menu { /** * Starts the list before the elements are added. * * Adds classes to the unordered list sub-menus. * * @param string $output Passed by reference. Used to append additional content. * @param int $depth Depth of menu item. Used for padding. * @param array $args An array of arguments. @see wp_nav_menu[] */ function start_lvl[ &$output, $depth = 0, $args = array[] ] { // Depth-dependent classes. $indent = [ $depth > 0 ? str_repeat[ "\t", $depth ] : '' ]; // code indent $display_depth = [ $depth + 1]; // because it counts the first submenu as 0 $classes = array[ 'sub-menu', [ $display_depth % 2 ? 'menu-odd' : 'menu-even' ], [ $display_depth >=2 ? 'sub-sub-menu' : '' ], 'menu-depth-' . $display_depth ]; $class_names = implode[ ' ', $classes ]; // Build HTML for output. $output .= "\n" . $indent . '
- ' . "\n";
}
/**
* Start the element output.
*
* Adds main/sub-classes to the list items and links.
*
* @param string $output Passed by reference. Used to append additional content.
* @param object $item Menu item data object.
* @param int $depth Depth of menu item. Used for padding.
* @param array $args An array of arguments. @see wp_nav_menu[]
* @param int $id Current item ID.
*/
function start_el[ &$output, $item, $depth = 0, $args = array[], $id = 0 ] {
global $wp_query;
$indent = [ $depth > 0 ? str_repeat[ "\t", $depth ] : '' ]; // code indent
// Depth-dependent classes.
$depth_classes = array[
[ $depth == 0 ? 'main-menu-item' : 'sub-menu-item' ],
[ $depth >=2 ? 'sub-sub-menu-item' : '' ],
[ $depth % 2 ? 'menu-item-odd' : 'menu-item-even' ],
'menu-item-depth-' . $depth
];
$depth_class_names = esc_attr[ implode[ ' ', $depth_classes ] ];
// Passed classes.
$classes = empty[ $item->classes ] ? array[] : [array] $item->classes;
$class_names = esc_attr[ implode[ ' ', apply_filters[ 'nav_menu_css_class', array_filter[ $classes ], $item ] ] ];
// Build HTML.
$output .= $indent . '
- '; // Link 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="' . esc_attr[ $item->url ] .'"' : ''; $attributes .= ' class="menu-link ' . [ $depth > 0 ? 'sub-menu-link' : 'main-menu-link' ] . '"'; // Build HTML output and pass through the proper filter. $item_output = sprintf[ '%1$s%3$s%4$s%5$s%6$s', $args->before, $attributes, $args->link_before, apply_filters[ 'the_title', $item->title, $item->ID ], $args->link_after, $args->after ]; $output .= apply_filters[ 'walker_nav_menu_start_el', $item_output, $item, $depth, $args ]; } }
Lớp này được thêm vào các mục menu tương ứng với cấp bậc gốc của loại hiện được hiển thị, trong đó {type} tương ứng với giá trị được sử dụng cho. menu-item-type-{type}.
06wp_nav_menu[ array[ 'menu' => 'Something custom walker', 'walker' => new WPDocs_Walker_Nav_Menu[] ] ]; /** * Custom walker class. */ class WPDocs_Walker_Nav_Menu extends Walker_Nav_Menu { /** * Starts the list before the elements are added. * * Adds classes to the unordered list sub-menus. * * @param string $output Passed by reference. Used to append additional content. * @param int $depth Depth of menu item. Used for padding. * @param array $args An array of arguments. @see wp_nav_menu[] */ function start_lvl[ &$output, $depth = 0, $args = array[] ] { // Depth-dependent classes. $indent = [ $depth > 0 ? str_repeat[ "\t", $depth ] : '' ]; // code indent $display_depth = [ $depth + 1]; // because it counts the first submenu as 0 $classes = array[ 'sub-menu', [ $display_depth % 2 ? 'menu-odd' : 'menu-even' ], [ $display_depth >=2 ? 'sub-sub-menu' : '' ], 'menu-depth-' . $display_depth ]; $class_names = implode[ ' ', $classes ]; // Build HTML for output. $output .= "\n" . $indent . '
- ' . "\n";
}
/**
* Start the element output.
*
* Adds main/sub-classes to the list items and links.
*
* @param string $output Passed by reference. Used to append additional content.
* @param object $item Menu item data object.
* @param int $depth Depth of menu item. Used for padding.
* @param array $args An array of arguments. @see wp_nav_menu[]
* @param int $id Current item ID.
*/
function start_el[ &$output, $item, $depth = 0, $args = array[], $id = 0 ] {
global $wp_query;
$indent = [ $depth > 0 ? str_repeat[ "\t", $depth ] : '' ]; // code indent
// Depth-dependent classes.
$depth_classes = array[
[ $depth == 0 ? 'main-menu-item' : 'sub-menu-item' ],
[ $depth >=2 ? 'sub-sub-menu-item' : '' ],
[ $depth % 2 ? 'menu-item-odd' : 'menu-item-even' ],
'menu-item-depth-' . $depth
];
$depth_class_names = esc_attr[ implode[ ' ', $depth_classes ] ];
// Passed classes.
$classes = empty[ $item->classes ] ? array[] : [array] $item->classes;
$class_names = esc_attr[ implode[ ' ', apply_filters[ 'nav_menu_css_class', array_filter[ $classes ], $item ] ] ];
// Build HTML.
$output .= $indent . '
- '; // Link 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="' . esc_attr[ $item->url ] .'"' : ''; $attributes .= ' class="menu-link ' . [ $depth > 0 ? 'sub-menu-link' : 'main-menu-link' ] . '"'; // Build HTML output and pass through the proper filter. $item_output = sprintf[ '%1$s%3$s%4$s%5$s%6$s', $args->before, $attributes, $args->link_before, apply_filters[ 'the_title', $item->title, $item->ID ], $args->link_after, $args->after ]; $output .= apply_filters[ 'walker_nav_menu_start_el', $item_output, $item, $depth, $args ]; } }
Lớp này được thêm vào các mục menu tương ứng với tổ tiên phân cấp của trang hiện được hiển thị.
07wp_nav_menu[ array[ 'menu' => 'Something custom walker', 'walker' => new WPDocs_Walker_Nav_Menu[] ] ]; /** * Custom walker class. */ class WPDocs_Walker_Nav_Menu extends Walker_Nav_Menu { /** * Starts the list before the elements are added. * * Adds classes to the unordered list sub-menus. * * @param string $output Passed by reference. Used to append additional content. * @param int $depth Depth of menu item. Used for padding. * @param array $args An array of arguments. @see wp_nav_menu[] */ function start_lvl[ &$output, $depth = 0, $args = array[] ] { // Depth-dependent classes. $indent = [ $depth > 0 ? str_repeat[ "\t", $depth ] : '' ]; // code indent $display_depth = [ $depth + 1]; // because it counts the first submenu as 0 $classes = array[ 'sub-menu', [ $display_depth % 2 ? 'menu-odd' : 'menu-even' ], [ $display_depth >=2 ? 'sub-sub-menu' : '' ], 'menu-depth-' . $display_depth ]; $class_names = implode[ ' ', $classes ]; // Build HTML for output. $output .= "\n" . $indent . '
- ' . "\n";
}
/**
* Start the element output.
*
* Adds main/sub-classes to the list items and links.
*
* @param string $output Passed by reference. Used to append additional content.
* @param object $item Menu item data object.
* @param int $depth Depth of menu item. Used for padding.
* @param array $args An array of arguments. @see wp_nav_menu[]
* @param int $id Current item ID.
*/
function start_el[ &$output, $item, $depth = 0, $args = array[], $id = 0 ] {
global $wp_query;
$indent = [ $depth > 0 ? str_repeat[ "\t", $depth ] : '' ]; // code indent
// Depth-dependent classes.
$depth_classes = array[
[ $depth == 0 ? 'main-menu-item' : 'sub-menu-item' ],
[ $depth >=2 ? 'sub-sub-menu-item' : '' ],
[ $depth % 2 ? 'menu-item-odd' : 'menu-item-even' ],
'menu-item-depth-' . $depth
];
$depth_class_names = esc_attr[ implode[ ' ', $depth_classes ] ];
// Passed classes.
$classes = empty[ $item->classes ] ? array[] : [array] $item->classes;
$class_names = esc_attr[ implode[ ' ', apply_filters[ 'nav_menu_css_class', array_filter[ $classes ], $item ] ] ];
// Build HTML.
$output .= $indent . '
- '; // Link 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="' . esc_attr[ $item->url ] .'"' : ''; $attributes .= ' class="menu-link ' . [ $depth > 0 ? 'sub-menu-link' : 'main-menu-link' ] . '"'; // Build HTML output and pass through the proper filter. $item_output = sprintf[ '%1$s%3$s%4$s%5$s%6$s', $args->before, $attributes, $args->link_before, apply_filters[ 'the_title', $item->title, $item->ID ], $args->link_after, $args->after ]; $output .= apply_filters[ 'walker_nav_menu_start_el', $item_output, $item, $depth, $args ]; } }
Lớp này được thêm vào các mục menu tương ứng với tổ tiên phân cấp của đối tượng hiện được hiển thị, trong đó {đối tượng} tương ứng với giá trị được sử dụng cho. menu-item-object-{object}.
08wp_nav_menu[ array[ 'menu' => 'Something custom walker', 'walker' => new WPDocs_Walker_Nav_Menu[] ] ]; /** * Custom walker class. */ class WPDocs_Walker_Nav_Menu extends Walker_Nav_Menu { /** * Starts the list before the elements are added. * * Adds classes to the unordered list sub-menus. * * @param string $output Passed by reference. Used to append additional content. * @param int $depth Depth of menu item. Used for padding. * @param array $args An array of arguments. @see wp_nav_menu[] */ function start_lvl[ &$output, $depth = 0, $args = array[] ] { // Depth-dependent classes. $indent = [ $depth > 0 ? str_repeat[ "\t", $depth ] : '' ]; // code indent $display_depth = [ $depth + 1]; // because it counts the first submenu as 0 $classes = array[ 'sub-menu', [ $display_depth % 2 ? 'menu-odd' : 'menu-even' ], [ $display_depth >=2 ? 'sub-sub-menu' : '' ], 'menu-depth-' . $display_depth ]; $class_names = implode[ ' ', $classes ]; // Build HTML for output. $output .= "\n" . $indent . '
- ' . "\n";
}
/**
* Start the element output.
*
* Adds main/sub-classes to the list items and links.
*
* @param string $output Passed by reference. Used to append additional content.
* @param object $item Menu item data object.
* @param int $depth Depth of menu item. Used for padding.
* @param array $args An array of arguments. @see wp_nav_menu[]
* @param int $id Current item ID.
*/
function start_el[ &$output, $item, $depth = 0, $args = array[], $id = 0 ] {
global $wp_query;
$indent = [ $depth > 0 ? str_repeat[ "\t", $depth ] : '' ]; // code indent
// Depth-dependent classes.
$depth_classes = array[
[ $depth == 0 ? 'main-menu-item' : 'sub-menu-item' ],
[ $depth >=2 ? 'sub-sub-menu-item' : '' ],
[ $depth % 2 ? 'menu-item-odd' : 'menu-item-even' ],
'menu-item-depth-' . $depth
];
$depth_class_names = esc_attr[ implode[ ' ', $depth_classes ] ];
// Passed classes.
$classes = empty[ $item->classes ] ? array[] : [array] $item->classes;
$class_names = esc_attr[ implode[ ' ', apply_filters[ 'nav_menu_css_class', array_filter[ $classes ], $item ] ] ];
// Build HTML.
$output .= $indent . '
- '; // Link 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="' . esc_attr[ $item->url ] .'"' : ''; $attributes .= ' class="menu-link ' . [ $depth > 0 ? 'sub-menu-link' : 'main-menu-link' ] . '"'; // Build HTML output and pass through the proper filter. $item_output = sprintf[ '%1$s%3$s%4$s%5$s%6$s', $args->before, $attributes, $args->link_before, apply_filters[ 'the_title', $item->title, $item->ID ], $args->link_after, $args->after ]; $output .= apply_filters[ 'walker_nav_menu_start_el', $item_output, $item, $depth, $args ]; } }
Lớp này được thêm vào các mục menu tương ứng với tổ tiên phân cấp của loại hiện được hiển thị, trong đó {type} tương ứng với giá trị được sử dụng cho. menu-item-type-{type}.
09wp_nav_menu[ array[ 'menu' => 'Something custom walker', 'walker' => new WPDocs_Walker_Nav_Menu[] ] ]; /** * Custom walker class. */ class WPDocs_Walker_Nav_Menu extends Walker_Nav_Menu { /** * Starts the list before the elements are added. * * Adds classes to the unordered list sub-menus. * * @param string $output Passed by reference. Used to append additional content. * @param int $depth Depth of menu item. Used for padding. * @param array $args An array of arguments. @see wp_nav_menu[] */ function start_lvl[ &$output, $depth = 0, $args = array[] ] { // Depth-dependent classes. $indent = [ $depth > 0 ? str_repeat[ "\t", $depth ] : '' ]; // code indent $display_depth = [ $depth + 1]; // because it counts the first submenu as 0 $classes = array[ 'sub-menu', [ $display_depth % 2 ? 'menu-odd' : 'menu-even' ], [ $display_depth >=2 ? 'sub-sub-menu' : '' ], 'menu-depth-' . $display_depth ]; $class_names = implode[ ' ', $classes ]; // Build HTML for output. $output .= "\n" . $indent . '
- ' . "\n";
}
/**
* Start the element output.
*
* Adds main/sub-classes to the list items and links.
*
* @param string $output Passed by reference. Used to append additional content.
* @param object $item Menu item data object.
* @param int $depth Depth of menu item. Used for padding.
* @param array $args An array of arguments. @see wp_nav_menu[]
* @param int $id Current item ID.
*/
function start_el[ &$output, $item, $depth = 0, $args = array[], $id = 0 ] {
global $wp_query;
$indent = [ $depth > 0 ? str_repeat[ "\t", $depth ] : '' ]; // code indent
// Depth-dependent classes.
$depth_classes = array[
[ $depth == 0 ? 'main-menu-item' : 'sub-menu-item' ],
[ $depth >=2 ? 'sub-sub-menu-item' : '' ],
[ $depth % 2 ? 'menu-item-odd' : 'menu-item-even' ],
'menu-item-depth-' . $depth
];
$depth_class_names = esc_attr[ implode[ ' ', $depth_classes ] ];
// Passed classes.
$classes = empty[ $item->classes ] ? array[] : [array] $item->classes;
$class_names = esc_attr[ implode[ ' ', apply_filters[ 'nav_menu_css_class', array_filter[ $classes ], $item ] ] ];
// Build HTML.
$output .= $indent . '
- '; // Link 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="' . esc_attr[ $item->url ] .'"' : ''; $attributes .= ' class="menu-link ' . [ $depth > 0 ? 'sub-menu-link' : 'main-menu-link' ] . '"'; // Build HTML output and pass through the proper filter. $item_output = sprintf[ '%1$s%3$s%4$s%5$s%6$s', $args->before, $attributes, $args->link_before, apply_filters[ 'the_title', $item->title, $item->ID ], $args->link_after, $args->after ]; $output .= apply_filters[ 'walker_nav_menu_start_el', $item_output, $item, $depth, $args ]; } }
Lớp này được thêm vào các mục menu tương ứng với trang đầu của trang web.
wp_page_menu[]
Các lớp sau đây được thêm vào để duy trì khả năng tương thích ngược với [[Tham chiếu hàm/wp_page_menu. đầu ra hàm wp_page_menu[] ]]
00wp_nav_menu[ array[ 'menu' => 'Something custom walker', 'walker' => new WPDocs_Walker_Nav_Menu[] ] ]; /** * Custom walker class. */ class WPDocs_Walker_Nav_Menu extends Walker_Nav_Menu { /** * Starts the list before the elements are added. * * Adds classes to the unordered list sub-menus. * * @param string $output Passed by reference. Used to append additional content. * @param int $depth Depth of menu item. Used for padding. * @param array $args An array of arguments. @see wp_nav_menu[] */ function start_lvl[ &$output, $depth = 0, $args = array[] ] { // Depth-dependent classes. $indent = [ $depth > 0 ? str_repeat[ "\t", $depth ] : '' ]; // code indent $display_depth = [ $depth + 1]; // because it counts the first submenu as 0 $classes = array[ 'sub-menu', [ $display_depth % 2 ? 'menu-odd' : 'menu-even' ], [ $display_depth >=2 ? 'sub-sub-menu' : '' ], 'menu-depth-' . $display_depth ]; $class_names = implode[ ' ', $classes ]; // Build HTML for output. $output .= "\n" . $indent . '
- ' . "\n";
}
/**
* Start the element output.
*
* Adds main/sub-classes to the list items and links.
*
* @param string $output Passed by reference. Used to append additional content.
* @param object $item Menu item data object.
* @param int $depth Depth of menu item. Used for padding.
* @param array $args An array of arguments. @see wp_nav_menu[]
* @param int $id Current item ID.
*/
function start_el[ &$output, $item, $depth = 0, $args = array[], $id = 0 ] {
global $wp_query;
$indent = [ $depth > 0 ? str_repeat[ "\t", $depth ] : '' ]; // code indent
// Depth-dependent classes.
$depth_classes = array[
[ $depth == 0 ? 'main-menu-item' : 'sub-menu-item' ],
[ $depth >=2 ? 'sub-sub-menu-item' : '' ],
[ $depth % 2 ? 'menu-item-odd' : 'menu-item-even' ],
'menu-item-depth-' . $depth
];
$depth_class_names = esc_attr[ implode[ ' ', $depth_classes ] ];
// Passed classes.
$classes = empty[ $item->classes ] ? array[] : [array] $item->classes;
$class_names = esc_attr[ implode[ ' ', apply_filters[ 'nav_menu_css_class', array_filter[ $classes ], $item ] ] ];
// Build HTML.
$output .= $indent . '
- '; // Link 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="' . esc_attr[ $item->url ] .'"' : ''; $attributes .= ' class="menu-link ' . [ $depth > 0 ? 'sub-menu-link' : 'main-menu-link' ] . '"'; // Build HTML output and pass through the proper filter. $item_output = sprintf[ '%1$s%3$s%4$s%5$s%6$s', $args->before, $attributes, $args->link_before, apply_filters[ 'the_title', $item->title, $item->ID ], $args->link_after, $args->after ]; $output .= apply_filters[ 'walker_nav_menu_start_el', $item_output, $item, $depth, $args ]; } }
Lớp này được thêm vào các mục menu tương ứng với trang tĩnh.
01wp_nav_menu[ array[ 'menu' => 'Something custom walker', 'walker' => new WPDocs_Walker_Nav_Menu[] ] ]; /** * Custom walker class. */ class WPDocs_Walker_Nav_Menu extends Walker_Nav_Menu { /** * Starts the list before the elements are added. * * Adds classes to the unordered list sub-menus. * * @param string $output Passed by reference. Used to append additional content. * @param int $depth Depth of menu item. Used for padding. * @param array $args An array of arguments. @see wp_nav_menu[] */ function start_lvl[ &$output, $depth = 0, $args = array[] ] { // Depth-dependent classes. $indent = [ $depth > 0 ? str_repeat[ "\t", $depth ] : '' ]; // code indent $display_depth = [ $depth + 1]; // because it counts the first submenu as 0 $classes = array[ 'sub-menu', [ $display_depth % 2 ? 'menu-odd' : 'menu-even' ], [ $display_depth >=2 ? 'sub-sub-menu' : '' ], 'menu-depth-' . $display_depth ]; $class_names = implode[ ' ', $classes ]; // Build HTML for output. $output .= "\n" . $indent . '
- ' . "\n";
}
/**
* Start the element output.
*
* Adds main/sub-classes to the list items and links.
*
* @param string $output Passed by reference. Used to append additional content.
* @param object $item Menu item data object.
* @param int $depth Depth of menu item. Used for padding.
* @param array $args An array of arguments. @see wp_nav_menu[]
* @param int $id Current item ID.
*/
function start_el[ &$output, $item, $depth = 0, $args = array[], $id = 0 ] {
global $wp_query;
$indent = [ $depth > 0 ? str_repeat[ "\t", $depth ] : '' ]; // code indent
// Depth-dependent classes.
$depth_classes = array[
[ $depth == 0 ? 'main-menu-item' : 'sub-menu-item' ],
[ $depth >=2 ? 'sub-sub-menu-item' : '' ],
[ $depth % 2 ? 'menu-item-odd' : 'menu-item-even' ],
'menu-item-depth-' . $depth
];
$depth_class_names = esc_attr[ implode[ ' ', $depth_classes ] ];
// Passed classes.
$classes = empty[ $item->classes ] ? array[] : [array] $item->classes;
$class_names = esc_attr[ implode[ ' ', apply_filters[ 'nav_menu_css_class', array_filter[ $classes ], $item ] ] ];
// Build HTML.
$output .= $indent . '
- '; // Link 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="' . esc_attr[ $item->url ] .'"' : ''; $attributes .= ' class="menu-link ' . [ $depth > 0 ? 'sub-menu-link' : 'main-menu-link' ] . '"'; // Build HTML output and pass through the proper filter. $item_output = sprintf[ '%1$s%3$s%4$s%5$s%6$s', $args->before, $attributes, $args->link_before, apply_filters[ 'the_title', $item->title, $item->ID ], $args->link_after, $args->after ]; $output .= apply_filters[ 'walker_nav_menu_start_el', $item_output, $item, $depth, $args ]; } }
Lớp này được thêm vào các mục menu có các trang phụ trong đó.
02wp_nav_menu[ array[ 'menu' => 'Something custom walker', 'walker' => new WPDocs_Walker_Nav_Menu[] ] ]; /** * Custom walker class. */ class WPDocs_Walker_Nav_Menu extends Walker_Nav_Menu { /** * Starts the list before the elements are added. * * Adds classes to the unordered list sub-menus. * * @param string $output Passed by reference. Used to append additional content. * @param int $depth Depth of menu item. Used for padding. * @param array $args An array of arguments. @see wp_nav_menu[] */ function start_lvl[ &$output, $depth = 0, $args = array[] ] { // Depth-dependent classes. $indent = [ $depth > 0 ? str_repeat[ "\t", $depth ] : '' ]; // code indent $display_depth = [ $depth + 1]; // because it counts the first submenu as 0 $classes = array[ 'sub-menu', [ $display_depth % 2 ? 'menu-odd' : 'menu-even' ], [ $display_depth >=2 ? 'sub-sub-menu' : '' ], 'menu-depth-' . $display_depth ]; $class_names = implode[ ' ', $classes ]; // Build HTML for output. $output .= "\n" . $indent . '
- ' . "\n";
}
/**
* Start the element output.
*
* Adds main/sub-classes to the list items and links.
*
* @param string $output Passed by reference. Used to append additional content.
* @param object $item Menu item data object.
* @param int $depth Depth of menu item. Used for padding.
* @param array $args An array of arguments. @see wp_nav_menu[]
* @param int $id Current item ID.
*/
function start_el[ &$output, $item, $depth = 0, $args = array[], $id = 0 ] {
global $wp_query;
$indent = [ $depth > 0 ? str_repeat[ "\t", $depth ] : '' ]; // code indent
// Depth-dependent classes.
$depth_classes = array[
[ $depth == 0 ? 'main-menu-item' : 'sub-menu-item' ],
[ $depth >=2 ? 'sub-sub-menu-item' : '' ],
[ $depth % 2 ? 'menu-item-odd' : 'menu-item-even' ],
'menu-item-depth-' . $depth
];
$depth_class_names = esc_attr[ implode[ ' ', $depth_classes ] ];
// Passed classes.
$classes = empty[ $item->classes ] ? array[] : [array] $item->classes;
$class_names = esc_attr[ implode[ ' ', apply_filters[ 'nav_menu_css_class', array_filter[ $classes ], $item ] ] ];
// Build HTML.
$output .= $indent . '
- '; // Link 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="' . esc_attr[ $item->url ] .'"' : ''; $attributes .= ' class="menu-link ' . [ $depth > 0 ? 'sub-menu-link' : 'main-menu-link' ] . '"'; // Build HTML output and pass through the proper filter. $item_output = sprintf[ '%1$s%3$s%4$s%5$s%6$s', $args->before, $attributes, $args->link_before, apply_filters[ 'the_title', $item->title, $item->ID ], $args->link_after, $args->after ]; $output .= apply_filters[ 'walker_nav_menu_start_el', $item_output, $item, $depth, $args ]; } }
Lớp này được thêm vào các mục menu tương ứng với trang tĩnh, trong đó $ID là ID trang tĩnh.
03wp_nav_menu[ array[ 'menu' => 'Something custom walker', 'walker' => new WPDocs_Walker_Nav_Menu[] ] ]; /** * Custom walker class. */ class WPDocs_Walker_Nav_Menu extends Walker_Nav_Menu { /** * Starts the list before the elements are added. * * Adds classes to the unordered list sub-menus. * * @param string $output Passed by reference. Used to append additional content. * @param int $depth Depth of menu item. Used for padding. * @param array $args An array of arguments. @see wp_nav_menu[] */ function start_lvl[ &$output, $depth = 0, $args = array[] ] { // Depth-dependent classes. $indent = [ $depth > 0 ? str_repeat[ "\t", $depth ] : '' ]; // code indent $display_depth = [ $depth + 1]; // because it counts the first submenu as 0 $classes = array[ 'sub-menu', [ $display_depth % 2 ? 'menu-odd' : 'menu-even' ], [ $display_depth >=2 ? 'sub-sub-menu' : '' ], 'menu-depth-' . $display_depth ]; $class_names = implode[ ' ', $classes ]; // Build HTML for output. $output .= "\n" . $indent . '
- ' . "\n";
}
/**
* Start the element output.
*
* Adds main/sub-classes to the list items and links.
*
* @param string $output Passed by reference. Used to append additional content.
* @param object $item Menu item data object.
* @param int $depth Depth of menu item. Used for padding.
* @param array $args An array of arguments. @see wp_nav_menu[]
* @param int $id Current item ID.
*/
function start_el[ &$output, $item, $depth = 0, $args = array[], $id = 0 ] {
global $wp_query;
$indent = [ $depth > 0 ? str_repeat[ "\t", $depth ] : '' ]; // code indent
// Depth-dependent classes.
$depth_classes = array[
[ $depth == 0 ? 'main-menu-item' : 'sub-menu-item' ],
[ $depth >=2 ? 'sub-sub-menu-item' : '' ],
[ $depth % 2 ? 'menu-item-odd' : 'menu-item-even' ],
'menu-item-depth-' . $depth
];
$depth_class_names = esc_attr[ implode[ ' ', $depth_classes ] ];
// Passed classes.
$classes = empty[ $item->classes ] ? array[] : [array] $item->classes;
$class_names = esc_attr[ implode[ ' ', apply_filters[ 'nav_menu_css_class', array_filter[ $classes ], $item ] ] ];
// Build HTML.
$output .= $indent . '
- '; // Link 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="' . esc_attr[ $item->url ] .'"' : ''; $attributes .= ' class="menu-link ' . [ $depth > 0 ? 'sub-menu-link' : 'main-menu-link' ] . '"'; // Build HTML output and pass through the proper filter. $item_output = sprintf[ '%1$s%3$s%4$s%5$s%6$s', $args->before, $attributes, $args->link_before, apply_filters[ 'the_title', $item->title, $item->ID ], $args->link_after, $args->after ]; $output .= apply_filters[ 'walker_nav_menu_start_el', $item_output, $item, $depth, $args ]; } }
Lớp này được thêm vào các mục menu tương ứng với trang tĩnh hiện được hiển thị.
04wp_nav_menu[ array[ 'menu' => 'Something custom walker', 'walker' => new WPDocs_Walker_Nav_Menu[] ] ]; /** * Custom walker class. */ class WPDocs_Walker_Nav_Menu extends Walker_Nav_Menu { /** * Starts the list before the elements are added. * * Adds classes to the unordered list sub-menus. * * @param string $output Passed by reference. Used to append additional content. * @param int $depth Depth of menu item. Used for padding. * @param array $args An array of arguments. @see wp_nav_menu[] */ function start_lvl[ &$output, $depth = 0, $args = array[] ] { // Depth-dependent classes. $indent = [ $depth > 0 ? str_repeat[ "\t", $depth ] : '' ]; // code indent $display_depth = [ $depth + 1]; // because it counts the first submenu as 0 $classes = array[ 'sub-menu', [ $display_depth % 2 ? 'menu-odd' : 'menu-even' ], [ $display_depth >=2 ? 'sub-sub-menu' : '' ], 'menu-depth-' . $display_depth ]; $class_names = implode[ ' ', $classes ]; // Build HTML for output. $output .= "\n" . $indent . '
- ' . "\n";
}
/**
* Start the element output.
*
* Adds main/sub-classes to the list items and links.
*
* @param string $output Passed by reference. Used to append additional content.
* @param object $item Menu item data object.
* @param int $depth Depth of menu item. Used for padding.
* @param array $args An array of arguments. @see wp_nav_menu[]
* @param int $id Current item ID.
*/
function start_el[ &$output, $item, $depth = 0, $args = array[], $id = 0 ] {
global $wp_query;
$indent = [ $depth > 0 ? str_repeat[ "\t", $depth ] : '' ]; // code indent
// Depth-dependent classes.
$depth_classes = array[
[ $depth == 0 ? 'main-menu-item' : 'sub-menu-item' ],
[ $depth >=2 ? 'sub-sub-menu-item' : '' ],
[ $depth % 2 ? 'menu-item-odd' : 'menu-item-even' ],
'menu-item-depth-' . $depth
];
$depth_class_names = esc_attr[ implode[ ' ', $depth_classes ] ];
// Passed classes.
$classes = empty[ $item->classes ] ? array[] : [array] $item->classes;
$class_names = esc_attr[ implode[ ' ', apply_filters[ 'nav_menu_css_class', array_filter[ $classes ], $item ] ] ];
// Build HTML.
$output .= $indent . '
- '; // Link 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="' . esc_attr[ $item->url ] .'"' : ''; $attributes .= ' class="menu-link ' . [ $depth > 0 ? 'sub-menu-link' : 'main-menu-link' ] . '"'; // Build HTML output and pass through the proper filter. $item_output = sprintf[ '%1$s%3$s%4$s%5$s%6$s', $args->before, $attributes, $args->link_before, apply_filters[ 'the_title', $item->title, $item->ID ], $args->link_after, $args->after ]; $output .= apply_filters[ 'walker_nav_menu_start_el', $item_output, $item, $depth, $args ]; } }
Lớp này được thêm vào các mục menu tương ứng với cấp bậc gốc của trang tĩnh hiện được hiển thị.
05wp_nav_menu[ array[ 'menu' => 'Something custom walker', 'walker' => new WPDocs_Walker_Nav_Menu[] ] ]; /** * Custom walker class. */ class WPDocs_Walker_Nav_Menu extends Walker_Nav_Menu { /** * Starts the list before the elements are added. * * Adds classes to the unordered list sub-menus. * * @param string $output Passed by reference. Used to append additional content. * @param int $depth Depth of menu item. Used for padding. * @param array $args An array of arguments. @see wp_nav_menu[] */ function start_lvl[ &$output, $depth = 0, $args = array[] ] { // Depth-dependent classes. $indent = [ $depth > 0 ? str_repeat[ "\t", $depth ] : '' ]; // code indent $display_depth = [ $depth + 1]; // because it counts the first submenu as 0 $classes = array[ 'sub-menu', [ $display_depth % 2 ? 'menu-odd' : 'menu-even' ], [ $display_depth >=2 ? 'sub-sub-menu' : '' ], 'menu-depth-' . $display_depth ]; $class_names = implode[ ' ', $classes ]; // Build HTML for output. $output .= "\n" . $indent . '
- ' . "\n";
}
/**
* Start the element output.
*
* Adds main/sub-classes to the list items and links.
*
* @param string $output Passed by reference. Used to append additional content.
* @param object $item Menu item data object.
* @param int $depth Depth of menu item. Used for padding.
* @param array $args An array of arguments. @see wp_nav_menu[]
* @param int $id Current item ID.
*/
function start_el[ &$output, $item, $depth = 0, $args = array[], $id = 0 ] {
global $wp_query;
$indent = [ $depth > 0 ? str_repeat[ "\t", $depth ] : '' ]; // code indent
// Depth-dependent classes.
$depth_classes = array[
[ $depth == 0 ? 'main-menu-item' : 'sub-menu-item' ],
[ $depth >=2 ? 'sub-sub-menu-item' : '' ],
[ $depth % 2 ? 'menu-item-odd' : 'menu-item-even' ],
'menu-item-depth-' . $depth
];
$depth_class_names = esc_attr[ implode[ ' ', $depth_classes ] ];
// Passed classes.
$classes = empty[ $item->classes ] ? array[] : [array] $item->classes;
$class_names = esc_attr[ implode[ ' ', apply_filters[ 'nav_menu_css_class', array_filter[ $classes ], $item ] ] ];
// Build HTML.
$output .= $indent . '
- '; // Link 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="' . esc_attr[ $item->url ] .'"' : ''; $attributes .= ' class="menu-link ' . [ $depth > 0 ? 'sub-menu-link' : 'main-menu-link' ] . '"'; // Build HTML output and pass through the proper filter. $item_output = sprintf[ '%1$s%3$s%4$s%5$s%6$s', $args->before, $attributes, $args->link_before, apply_filters[ 'the_title', $item->title, $item->ID ], $args->link_after, $args->after ]; $output .= apply_filters[ 'walker_nav_menu_start_el', $item_output, $item, $depth, $args ]; } }
Lớp này được thêm vào các mục menu tương ứng với tổ tiên phân cấp của trang tĩnh hiện được hiển thị.
Tập tin.
wp_nav_menu[ array[
'menu' => 'Something custom walker',
'walker' => new WPDocs_Walker_Nav_Menu[]
] ];
/**
* Custom walker class.
*/
class WPDocs_Walker_Nav_Menu extends Walker_Nav_Menu {
/**
* Starts the list before the elements are added.
*
* Adds classes to the unordered list sub-menus.
*
* @param string $output Passed by reference. Used to append additional content.
* @param int $depth Depth of menu item. Used for padding.
* @param array $args An array of arguments. @see wp_nav_menu[]
*/
function start_lvl[ &$output, $depth = 0, $args = array[] ] {
// Depth-dependent classes.
$indent = [ $depth > 0 ? str_repeat[ "\t", $depth ] : '' ]; // code indent
$display_depth = [ $depth + 1]; // because it counts the first submenu as 0
$classes = array[
'sub-menu',
[ $display_depth % 2 ? 'menu-odd' : 'menu-even' ],
[ $display_depth >=2 ? 'sub-sub-menu' : '' ],
'menu-depth-' . $display_depth
];
$class_names = implode[ ' ', $classes ];
// Build HTML for output.
$output .= "\n" . $indent . '' . "\n";
}
/**
* Start the element output.
*
* Adds main/sub-classes to the list items and links.
*
* @param string $output Passed by reference. Used to append additional content.
* @param object $item Menu item data object.
* @param int $depth Depth of menu item. Used for padding.
* @param array $args An array of arguments. @see wp_nav_menu[]
* @param int $id Current item ID.
*/
function start_el[ &$output, $item, $depth = 0, $args = array[], $id = 0 ] {
global $wp_query;
$indent = [ $depth > 0 ? str_repeat[ "\t", $depth ] : '' ]; // code indent
// Depth-dependent classes.
$depth_classes = array[
[ $depth == 0 ? 'main-menu-item' : 'sub-menu-item' ],
[ $depth >=2 ? 'sub-sub-menu-item' : '' ],
[ $depth % 2 ? 'menu-item-odd' : 'menu-item-even' ],
'menu-item-depth-' . $depth
];
$depth_class_names = esc_attr[ implode[ ' ', $depth_classes ] ];
// Passed classes.
$classes = empty[ $item->classes ] ? array[] : [array] $item->classes;
$class_names = esc_attr[ implode[ ' ', apply_filters[ 'nav_menu_css_class', array_filter[ $classes ], $item ] ] ];
// Build HTML.
$output .= $indent . '- ';
// Link 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="' . esc_attr[ $item->url ] .'"' : '';
$attributes .= ' class="menu-link ' . [ $depth > 0 ? 'sub-menu-link' : 'main-menu-link' ] . '"';
// Build HTML output and pass through the proper filter.
$item_output = sprintf[ '%1$s%3$s%4$s%5$s%6$s',
$args->before,
$attributes,
$args->link_before,
apply_filters[ 'the_title', $item->title, $item->ID ],
$args->link_after,
$args->after
];
$output .= apply_filters[ 'walker_nav_menu_start_el', $item_output, $item, $depth, $args ];
}
}
06. Xem tất cả tài liệu tham khảowp_nav_menu[ array[
'menu' => 'Something custom walker',
'walker' => new WPDocs_Walker_Nav_Menu[]
] ];
/**
* Custom walker class.
*/
class WPDocs_Walker_Nav_Menu extends Walker_Nav_Menu {
/**
* Starts the list before the elements are added.
*
* Adds classes to the unordered list sub-menus.
*
* @param string $output Passed by reference. Used to append additional content.
* @param int $depth Depth of menu item. Used for padding.
* @param array $args An array of arguments. @see wp_nav_menu[]
*/
function start_lvl[ &$output, $depth = 0, $args = array[] ] {
// Depth-dependent classes.
$indent = [ $depth > 0 ? str_repeat[ "\t", $depth ] : '' ]; // code indent
$display_depth = [ $depth + 1]; // because it counts the first submenu as 0
$classes = array[
'sub-menu',
[ $display_depth % 2 ? 'menu-odd' : 'menu-even' ],
[ $display_depth >=2 ? 'sub-sub-menu' : '' ],
'menu-depth-' . $display_depth
];
$class_names = implode[ ' ', $classes ];
// Build HTML for output.
$output .= "\n" . $indent . '' . "\n";
}
/**
* Start the element output.
*
* Adds main/sub-classes to the list items and links.
*
* @param string $output Passed by reference. Used to append additional content.
* @param object $item Menu item data object.
* @param int $depth Depth of menu item. Used for padding.
* @param array $args An array of arguments. @see wp_nav_menu[]
* @param int $id Current item ID.
*/
function start_el[ &$output, $item, $depth = 0, $args = array[], $id = 0 ] {
global $wp_query;
$indent = [ $depth > 0 ? str_repeat[ "\t", $depth ] : '' ]; // code indent
// Depth-dependent classes.
$depth_classes = array[
[ $depth == 0 ? 'main-menu-item' : 'sub-menu-item' ],
[ $depth >=2 ? 'sub-sub-menu-item' : '' ],
[ $depth % 2 ? 'menu-item-odd' : 'menu-item-even' ],
'menu-item-depth-' . $depth
];
$depth_class_names = esc_attr[ implode[ ' ', $depth_classes ] ];
// Passed classes.
$classes = empty[ $item->classes ] ? array[] : [array] $item->classes;
$class_names = esc_attr[ implode[ ' ', apply_filters[ 'nav_menu_css_class', array_filter[ $classes ], $item ] ] ];
// Build HTML.
$output .= $indent . '- ';
// Link 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="' . esc_attr[ $item->url ] .'"' : '';
$attributes .= ' class="menu-link ' . [ $depth > 0 ? 'sub-menu-link' : 'main-menu-link' ] . '"';
// Build HTML output and pass through the proper filter.
$item_output = sprintf[ '%1$s%3$s%4$s%5$s%6$s',
$args->before,
$attributes,
$args->link_before,
apply_filters[ 'the_title', $item->title, $item->ID ],
$args->link_after,
$args->after
];
$output .= apply_filters[ 'walker_nav_menu_start_el', $item_output, $item, $depth, $args ];
}
}
7
wp_nav_menu[ array[
'menu' => 'Something custom walker',
'walker' => new WPDocs_Walker_Nav_Menu[]
] ];
/**
* Custom walker class.
*/
class WPDocs_Walker_Nav_Menu extends Walker_Nav_Menu {
/**
* Starts the list before the elements are added.
*
* Adds classes to the unordered list sub-menus.
*
* @param string $output Passed by reference. Used to append additional content.
* @param int $depth Depth of menu item. Used for padding.
* @param array $args An array of arguments. @see wp_nav_menu[]
*/
function start_lvl[ &$output, $depth = 0, $args = array[] ] {
// Depth-dependent classes.
$indent = [ $depth > 0 ? str_repeat[ "\t", $depth ] : '' ]; // code indent
$display_depth = [ $depth + 1]; // because it counts the first submenu as 0
$classes = array[
'sub-menu',
[ $display_depth % 2 ? 'menu-odd' : 'menu-even' ],
[ $display_depth >=2 ? 'sub-sub-menu' : '' ],
'menu-depth-' . $display_depth
];
$class_names = implode[ ' ', $classes ];
// Build HTML for output.
$output .= "\n" . $indent . '' . "\n";
}
/**
* Start the element output.
*
* Adds main/sub-classes to the list items and links.
*
* @param string $output Passed by reference. Used to append additional content.
* @param object $item Menu item data object.
* @param int $depth Depth of menu item. Used for padding.
* @param array $args An array of arguments. @see wp_nav_menu[]
* @param int $id Current item ID.
*/
function start_el[ &$output, $item, $depth = 0, $args = array[], $id = 0 ] {
global $wp_query;
$indent = [ $depth > 0 ? str_repeat[ "\t", $depth ] : '' ]; // code indent
// Depth-dependent classes.
$depth_classes = array[
[ $depth == 0 ? 'main-menu-item' : 'sub-menu-item' ],
[ $depth >=2 ? 'sub-sub-menu-item' : '' ],
[ $depth % 2 ? 'menu-item-odd' : 'menu-item-even' ],
'menu-item-depth-' . $depth
];
$depth_class_names = esc_attr[ implode[ ' ', $depth_classes ] ];
// Passed classes.
$classes = empty[ $item->classes ] ? array[] : [array] $item->classes;
$class_names = esc_attr[ implode[ ' ', apply_filters[ 'nav_menu_css_class', array_filter[ $classes ], $item ] ] ];
// Build HTML.
$output .= $indent . '- ';
// Link 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="' . esc_attr[ $item->url ] .'"' : '';
$attributes .= ' class="menu-link ' . [ $depth > 0 ? 'sub-menu-link' : 'main-menu-link' ] . '"';
// Build HTML output and pass through the proper filter.
$item_output = sprintf[ '%1$s%3$s%4$s%5$s%6$s',
$args->before,
$attributes,
$args->link_before,
apply_filters[ 'the_title', $item->title, $item->ID ],
$args->link_after,
$args->after
];
$output .= apply_filters[ 'walker_nav_menu_start_el', $item_output, $item, $depth, $args ];
}
}
4. 3. 0. 0Giới thiệu. - 0 ? 'sub-menu-link' : 'main-menu-link' ] . '"';
// Build HTML output and pass through the proper filter.
$item_output = sprintf[ '%1$s%3$s%4$s%5$s%6$s',
$args->before,
$attributes,
$args->link_before,
apply_filters[ 'the_title', $item->title, $item->ID ],
$args->link_after,
$args->after
];
$output .= apply_filters[ 'walker_nav_menu_start_el', $item_output, $item, $depth, $args ];
}
}0
- 0 ? 'sub-menu-link' : 'main-menu-link' ] . '"'; // Build HTML output and pass through the proper filter. $item_output = sprintf[ '%1$s%3$s%4$s%5$s%6$s', $args->before, $attributes, $args->link_before, apply_filters[ 'the_title', $item->title, $item->ID ], $args->link_after, $args->after ]; $output .= apply_filters[ 'walker_nav_menu_start_el', $item_output, $item, $depth, $args ]; } }1Đăng nhập để thêm phản hồi
- 0 ? 'sub-menu-link' : 'main-menu-link' ] . '"';
// Build HTML output and pass through the proper filter.
$item_output = sprintf[ '%1$s%3$s%4$s%5$s%6$s',
$args->before,
$attributes,
$args->link_before,
apply_filters[ 'the_title', $item->title, $item->ID ],
$args->link_after,
$args->after
];
$output .= apply_filters[ 'walker_nav_menu_start_el', $item_output, $item, $depth, $args ];
}
}09
Ví dụ này sẽ loại bỏ danh sách không có thứ tự bao quanh các mục danh sách. Việc thay thế HTML bọc bằng trình xác định
30 dẫn đến việc chỉ xuất nội dung danh sách HTML cho các mục menu, vìwp_nav_menu[ array[ 'menu' => 'Something custom walker', 'walker' => new WPDocs_Walker_Nav_Menu[] ] ]; /** * Custom walker class. */ class WPDocs_Walker_Nav_Menu extends Walker_Nav_Menu { /** * Starts the list before the elements are added. * * Adds classes to the unordered list sub-menus. * * @param string $output Passed by reference. Used to append additional content. * @param int $depth Depth of menu item. Used for padding. * @param array $args An array of arguments. @see wp_nav_menu[] */ function start_lvl[ &$output, $depth = 0, $args = array[] ] { // Depth-dependent classes. $indent = [ $depth > 0 ? str_repeat[ "\t", $depth ] : '' ]; // code indent $display_depth = [ $depth + 1]; // because it counts the first submenu as 0 $classes = array[ 'sub-menu', [ $display_depth % 2 ? 'menu-odd' : 'menu-even' ], [ $display_depth >=2 ? 'sub-sub-menu' : '' ], 'menu-depth-' . $display_depth ]; $class_names = implode[ ' ', $classes ]; // Build HTML for output. $output .= "\n" . $indent . '
- ' . "\n";
}
/**
* Start the element output.
*
* Adds main/sub-classes to the list items and links.
*
* @param string $output Passed by reference. Used to append additional content.
* @param object $item Menu item data object.
* @param int $depth Depth of menu item. Used for padding.
* @param array $args An array of arguments. @see wp_nav_menu[]
* @param int $id Current item ID.
*/
function start_el[ &$output, $item, $depth = 0, $args = array[], $id = 0 ] {
global $wp_query;
$indent = [ $depth > 0 ? str_repeat[ "\t", $depth ] : '' ]; // code indent
// Depth-dependent classes.
$depth_classes = array[
[ $depth == 0 ? 'main-menu-item' : 'sub-menu-item' ],
[ $depth >=2 ? 'sub-sub-menu-item' : '' ],
[ $depth % 2 ? 'menu-item-odd' : 'menu-item-even' ],
'menu-item-depth-' . $depth
];
$depth_class_names = esc_attr[ implode[ ' ', $depth_classes ] ];
// Passed classes.
$classes = empty[ $item->classes ] ? array[] : [array] $item->classes;
$class_names = esc_attr[ implode[ ' ', apply_filters[ 'nav_menu_css_class', array_filter[ $classes ], $item ] ] ];
// Build HTML.
$output .= $indent . '
- '; // Link 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="' . esc_attr[ $item->url ] .'"' : ''; $attributes .= ' class="menu-link ' . [ $depth > 0 ? 'sub-menu-link' : 'main-menu-link' ] . '"'; // Build HTML output and pass through the proper filter. $item_output = sprintf[ '%1$s%3$s%4$s%5$s%6$s', $args->before, $attributes, $args->link_before, apply_filters[ 'the_title', $item->title, $item->ID ], $args->link_after, $args->after ]; $output .= apply_filters[ 'walker_nav_menu_start_el', $item_output, $item, $depth, $args ]; } }
3 được tạo bằng cách sử dụngwp_nav_menu[ array[ 'menu' => 'Something custom walker', 'walker' => new WPDocs_Walker_Nav_Menu[] ] ]; /** * Custom walker class. */ class WPDocs_Walker_Nav_Menu extends Walker_Nav_Menu { /** * Starts the list before the elements are added. * * Adds classes to the unordered list sub-menus. * * @param string $output Passed by reference. Used to append additional content. * @param int $depth Depth of menu item. Used for padding. * @param array $args An array of arguments. @see wp_nav_menu[] */ function start_lvl[ &$output, $depth = 0, $args = array[] ] { // Depth-dependent classes. $indent = [ $depth > 0 ? str_repeat[ "\t", $depth ] : '' ]; // code indent $display_depth = [ $depth + 1]; // because it counts the first submenu as 0 $classes = array[ 'sub-menu', [ $display_depth % 2 ? 'menu-odd' : 'menu-even' ], [ $display_depth >=2 ? 'sub-sub-menu' : '' ], 'menu-depth-' . $display_depth ]; $class_names = implode[ ' ', $classes ]; // Build HTML for output. $output .= "\n" . $indent . '
- ' . "\n";
}
/**
* Start the element output.
*
* Adds main/sub-classes to the list items and links.
*
* @param string $output Passed by reference. Used to append additional content.
* @param object $item Menu item data object.
* @param int $depth Depth of menu item. Used for padding.
* @param array $args An array of arguments. @see wp_nav_menu[]
* @param int $id Current item ID.
*/
function start_el[ &$output, $item, $depth = 0, $args = array[], $id = 0 ] {
global $wp_query;
$indent = [ $depth > 0 ? str_repeat[ "\t", $depth ] : '' ]; // code indent
// Depth-dependent classes.
$depth_classes = array[
[ $depth == 0 ? 'main-menu-item' : 'sub-menu-item' ],
[ $depth >=2 ? 'sub-sub-menu-item' : '' ],
[ $depth % 2 ? 'menu-item-odd' : 'menu-item-even' ],
'menu-item-depth-' . $depth
];
$depth_class_names = esc_attr[ implode[ ' ', $depth_classes ] ];
// Passed classes.
$classes = empty[ $item->classes ] ? array[] : [array] $item->classes;
$class_names = esc_attr[ implode[ ' ', apply_filters[ 'nav_menu_css_class', array_filter[ $classes ], $item ] ] ];
// Build HTML.
$output .= $indent . '
- '; // Link 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="' . esc_attr[ $item->url ] .'"' : ''; $attributes .= ' class="menu-link ' . [ $depth > 0 ? 'sub-menu-link' : 'main-menu-link' ] . '"'; // Build HTML output and pass through the proper filter. $item_output = sprintf[ '%1$s%3$s%4$s%5$s%6$s', $args->before, $attributes, $args->link_before, apply_filters[ 'the_title', $item->title, $item->ID ], $args->link_after, $args->after ]; $output .= apply_filters[ 'walker_nav_menu_start_el', $item_output, $item, $depth, $args ]; } }
32wp_nav_menu[ array[ 'menu' => 'Something custom walker', 'walker' => new WPDocs_Walker_Nav_Menu[] ] ]; /** * Custom walker class. */ class WPDocs_Walker_Nav_Menu extends Walker_Nav_Menu { /** * Starts the list before the elements are added. * * Adds classes to the unordered list sub-menus. * * @param string $output Passed by reference. Used to append additional content. * @param int $depth Depth of menu item. Used for padding. * @param array $args An array of arguments. @see wp_nav_menu[] */ function start_lvl[ &$output, $depth = 0, $args = array[] ] { // Depth-dependent classes. $indent = [ $depth > 0 ? str_repeat[ "\t", $depth ] : '' ]; // code indent $display_depth = [ $depth + 1]; // because it counts the first submenu as 0 $classes = array[ 'sub-menu', [ $display_depth % 2 ? 'menu-odd' : 'menu-even' ], [ $display_depth >=2 ? 'sub-sub-menu' : '' ], 'menu-depth-' . $display_depth ]; $class_names = implode[ ' ', $classes ]; // Build HTML for output. $output .= "\n" . $indent . '
- ' . "\n";
}
/**
* Start the element output.
*
* Adds main/sub-classes to the list items and links.
*
* @param string $output Passed by reference. Used to append additional content.
* @param object $item Menu item data object.
* @param int $depth Depth of menu item. Used for padding.
* @param array $args An array of arguments. @see wp_nav_menu[]
* @param int $id Current item ID.
*/
function start_el[ &$output, $item, $depth = 0, $args = array[], $id = 0 ] {
global $wp_query;
$indent = [ $depth > 0 ? str_repeat[ "\t", $depth ] : '' ]; // code indent
// Depth-dependent classes.
$depth_classes = array[
[ $depth == 0 ? 'main-menu-item' : 'sub-menu-item' ],
[ $depth >=2 ? 'sub-sub-menu-item' : '' ],
[ $depth % 2 ? 'menu-item-odd' : 'menu-item-even' ],
'menu-item-depth-' . $depth
];
$depth_class_names = esc_attr[ implode[ ' ', $depth_classes ] ];
// Passed classes.
$classes = empty[ $item->classes ] ? array[] : [array] $item->classes;
$class_names = esc_attr[ implode[ ' ', apply_filters[ 'nav_menu_css_class', array_filter[ $classes ], $item ] ] ];
// Build HTML.
$output .= $indent . '
- '; // Link 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="' . esc_attr[ $item->url ] .'"' : ''; $attributes .= ' class="menu-link ' . [ $depth > 0 ? 'sub-menu-link' : 'main-menu-link' ] . '"'; // Build HTML output and pass through the proper filter. $item_output = sprintf[ '%1$s%3$s%4$s%5$s%6$s', $args->before, $attributes, $args->link_before, apply_filters[ 'the_title', $item->title, $item->ID ], $args->link_after, $args->after ]; $output .= apply_filters[ 'walker_nav_menu_start_el', $item_output, $item, $depth, $args ]; } }
3 mặc định trước.wp_nav_menu[ array[ 'menu' => 'Something custom walker', 'walker' => new WPDocs_Walker_Nav_Menu[] ] ]; /** * Custom walker class. */ class WPDocs_Walker_Nav_Menu extends Walker_Nav_Menu { /** * Starts the list before the elements are added. * * Adds classes to the unordered list sub-menus. * * @param string $output Passed by reference. Used to append additional content. * @param int $depth Depth of menu item. Used for padding. * @param array $args An array of arguments. @see wp_nav_menu[] */ function start_lvl[ &$output, $depth = 0, $args = array[] ] { // Depth-dependent classes. $indent = [ $depth > 0 ? str_repeat[ "\t", $depth ] : '' ]; // code indent $display_depth = [ $depth + 1]; // because it counts the first submenu as 0 $classes = array[ 'sub-menu', [ $display_depth % 2 ? 'menu-odd' : 'menu-even' ], [ $display_depth >=2 ? 'sub-sub-menu' : '' ], 'menu-depth-' . $display_depth ]; $class_names = implode[ ' ', $classes ]; // Build HTML for output. $output .= "\n" . $indent . '
- ' . "\n";
}
/**
* Start the element output.
*
* Adds main/sub-classes to the list items and links.
*
* @param string $output Passed by reference. Used to append additional content.
* @param object $item Menu item data object.
* @param int $depth Depth of menu item. Used for padding.
* @param array $args An array of arguments. @see wp_nav_menu[]
* @param int $id Current item ID.
*/
function start_el[ &$output, $item, $depth = 0, $args = array[], $id = 0 ] {
global $wp_query;
$indent = [ $depth > 0 ? str_repeat[ "\t", $depth ] : '' ]; // code indent
// Depth-dependent classes.
$depth_classes = array[
[ $depth == 0 ? 'main-menu-item' : 'sub-menu-item' ],
[ $depth >=2 ? 'sub-sub-menu-item' : '' ],
[ $depth % 2 ? 'menu-item-odd' : 'menu-item-even' ],
'menu-item-depth-' . $depth
];
$depth_class_names = esc_attr[ implode[ ' ', $depth_classes ] ];
// Passed classes.
$classes = empty[ $item->classes ] ? array[] : [array] $item->classes;
$class_names = esc_attr[ implode[ ' ', apply_filters[ 'nav_menu_css_class', array_filter[ $classes ], $item ] ] ];
// Build HTML.
$output .= $indent . '
- '; // Link 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="' . esc_attr[ $item->url ] .'"' : ''; $attributes .= ' class="menu-link ' . [ $depth > 0 ? 'sub-menu-link' : 'main-menu-link' ] . '"'; // Build HTML output and pass through the proper filter. $item_output = sprintf[ '%1$s%3$s%4$s%5$s%6$s', $args->before, $attributes, $args->link_before, apply_filters[ 'the_title', $item->title, $item->ID ], $args->link_after, $args->after ]; $output .= apply_filters[ 'walker_nav_menu_start_el', $item_output, $item, $depth, $args ]; } }
[html]
'Something custom walker', 'walker' => new WPDocs_Walker_Nav_Menu[] ] ]; /** * Custom walker class. */ class WPDocs_Walker_Nav_Menu extends Walker_Nav_Menu { /** * Starts the list before the elements are added. * * Adds classes to the unordered list sub-menus. * * @param string $output Passed by reference. Used to append additional content. * @param int $depth Depth of menu item. Used for padding. * @param array $args An array of arguments. @see wp_nav_menu[] */ function start_lvl[ &$output, $depth = 0, $args = array[] ] { // Depth-dependent classes. $indent = [ $depth > 0 ? str_repeat[ "\t", $depth ] : '' ]; // code indent $display_depth = [ $depth + 1]; // because it counts the first submenu as 0 $classes = array[ 'sub-menu', [ $display_depth % 2 ? 'menu-odd' : 'menu-even' ], [ $display_depth >=2 ? 'sub-sub-menu' : '' ], 'menu-depth-' . $display_depth ]; $class_names = implode[ ' ', $classes ]; // Build HTML for output. $output .= "\n" . $indent . '- ' . "\n";
}
/**
* Start the element output.
*
* Adds main/sub-classes to the list items and links.
*
* @param string $output Passed by reference. Used to append additional content.
* @param object $item Menu item data object.
* @param int $depth Depth of menu item. Used for padding.
* @param array $args An array of arguments. @see wp_nav_menu[]
* @param int $id Current item ID.
*/
function start_el[ &$output, $item, $depth = 0, $args = array[], $id = 0 ] {
global $wp_query;
$indent = [ $depth > 0 ? str_repeat[ "\t", $depth ] : '' ]; // code indent
// Depth-dependent classes.
$depth_classes = array[
[ $depth == 0 ? 'main-menu-item' : 'sub-menu-item' ],
[ $depth >=2 ? 'sub-sub-menu-item' : '' ],
[ $depth % 2 ? 'menu-item-odd' : 'menu-item-even' ],
'menu-item-depth-' . $depth
];
$depth_class_names = esc_attr[ implode[ ' ', $depth_classes ] ];
// Passed classes.
$classes = empty[ $item->classes ] ? array[] : [array] $item->classes;
$class_names = esc_attr[ implode[ ' ', apply_filters[ 'nav_menu_css_class', array_filter[ $classes ], $item ] ] ];
// Build HTML.
$output .= $indent . '
- ';
// Link 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="' . esc_attr[ $item->url ] .'"' : '';
$attributes .= ' class="menu-link ' . [ $depth > 0 ? 'sub-menu-link' : 'main-menu-link' ] . '"';
// Build HTML output and pass through the proper filter.
$item_output = sprintf[ '%1$s%3$s%4$s%5$s%6$s',
$args->before,
$attributes,
$args->link_before,
apply_filters[ 'the_title', $item->title, $item->ID ],
$args->link_after,
$args->after
];
$output .= apply_filters[ 'walker_nav_menu_start_el', $item_output, $item, $depth, $args ];
}
}3 sau.
[html]
%3$s
[/html]Ví dụ
8Đăng nhập để thêm phản hồiwp_nav_menu[ array[ 'menu' => 'Something custom walker', 'walker' => new WPDocs_Walker_Nav_Menu[] ] ]; /** * Custom walker class. */ class WPDocs_Walker_Nav_Menu extends Walker_Nav_Menu { /** * Starts the list before the elements are added. * * Adds classes to the unordered list sub-menus. * * @param string $output Passed by reference. Used to append additional content. * @param int $depth Depth of menu item. Used for padding. * @param array $args An array of arguments. @see wp_nav_menu[] */ function start_lvl[ &$output, $depth = 0, $args = array[] ] { // Depth-dependent classes. $indent = [ $depth > 0 ? str_repeat[ "\t", $depth ] : '' ]; // code indent $display_depth = [ $depth + 1]; // because it counts the first submenu as 0 $classes = array[ 'sub-menu', [ $display_depth % 2 ? 'menu-odd' : 'menu-even' ], [ $display_depth >=2 ? 'sub-sub-menu' : '' ], 'menu-depth-' . $display_depth ]; $class_names = implode[ ' ', $classes ]; // Build HTML for output. $output .= "\n" . $indent . '
- ' . "\n";
}
/**
* Start the element output.
*
* Adds main/sub-classes to the list items and links.
*
* @param string $output Passed by reference. Used to append additional content.
* @param object $item Menu item data object.
* @param int $depth Depth of menu item. Used for padding.
* @param array $args An array of arguments. @see wp_nav_menu[]
* @param int $id Current item ID.
*/
function start_el[ &$output, $item, $depth = 0, $args = array[], $id = 0 ] {
global $wp_query;
$indent = [ $depth > 0 ? str_repeat[ "\t", $depth ] : '' ]; // code indent
// Depth-dependent classes.
$depth_classes = array[
[ $depth == 0 ? 'main-menu-item' : 'sub-menu-item' ],
[ $depth >=2 ? 'sub-sub-menu-item' : '' ],
[ $depth % 2 ? 'menu-item-odd' : 'menu-item-even' ],
'menu-item-depth-' . $depth
];
$depth_class_names = esc_attr[ implode[ ' ', $depth_classes ] ];
// Passed classes.
$classes = empty[ $item->classes ] ? array[] : [array] $item->classes;
$class_names = esc_attr[ implode[ ' ', apply_filters[ 'nav_menu_css_class', array_filter[ $classes ], $item ] ] ];
// Build HTML.
$output .= $indent . '
- '; // Link 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="' . esc_attr[ $item->url ] .'"' : ''; $attributes .= ' class="menu-link ' . [ $depth > 0 ? 'sub-menu-link' : 'main-menu-link' ] . '"'; // Build HTML output and pass through the proper filter. $item_output = sprintf[ '%1$s%3$s%4$s%5$s%6$s', $args->before, $attributes, $args->link_before, apply_filters[ 'the_title', $item->title, $item->ID ], $args->link_after, $args->after ]; $output .= apply_filters[ 'walker_nav_menu_start_el', $item_output, $item, $depth, $args ]; } }
- 0 ? 'sub-menu-link' : 'main-menu-link' ] . '"';
// Build HTML output and pass through the proper filter.
$item_output = sprintf[ '%1$s%3$s%4$s%5$s%6$s',
$args->before,
$attributes,
$args->link_before,
apply_filters[ 'the_title', $item->title, $item->ID ],
$args->link_after,
$args->after
];
$output .= apply_filters[ 'walker_nav_menu_start_el', $item_output, $item, $depth, $args ];
}
}35]
Cách đơn giản nhất để xây dựng chức năng walker mới là sao chép và mở rộng lớp mặc định [
36] từ /wp-includes/nav-menu-template. php và chỉ cần tùy chỉnh những gì bạn cầnwp_nav_menu[ array[ 'menu' => 'Something custom walker', 'walker' => new WPDocs_Walker_Nav_Menu[] ] ]; /** * Custom walker class. */ class WPDocs_Walker_Nav_Menu extends Walker_Nav_Menu { /** * Starts the list before the elements are added. * * Adds classes to the unordered list sub-menus. * * @param string $output Passed by reference. Used to append additional content. * @param int $depth Depth of menu item. Used for padding. * @param array $args An array of arguments. @see wp_nav_menu[] */ function start_lvl[ &$output, $depth = 0, $args = array[] ] { // Depth-dependent classes. $indent = [ $depth > 0 ? str_repeat[ "\t", $depth ] : '' ]; // code indent $display_depth = [ $depth + 1]; // because it counts the first submenu as 0 $classes = array[ 'sub-menu', [ $display_depth % 2 ? 'menu-odd' : 'menu-even' ], [ $display_depth >=2 ? 'sub-sub-menu' : '' ], 'menu-depth-' . $display_depth ]; $class_names = implode[ ' ', $classes ]; // Build HTML for output. $output .= "\n" . $indent . '
- ' . "\n";
}
/**
* Start the element output.
*
* Adds main/sub-classes to the list items and links.
*
* @param string $output Passed by reference. Used to append additional content.
* @param object $item Menu item data object.
* @param int $depth Depth of menu item. Used for padding.
* @param array $args An array of arguments. @see wp_nav_menu[]
* @param int $id Current item ID.
*/
function start_el[ &$output, $item, $depth = 0, $args = array[], $id = 0 ] {
global $wp_query;
$indent = [ $depth > 0 ? str_repeat[ "\t", $depth ] : '' ]; // code indent
// Depth-dependent classes.
$depth_classes = array[
[ $depth == 0 ? 'main-menu-item' : 'sub-menu-item' ],
[ $depth >=2 ? 'sub-sub-menu-item' : '' ],
[ $depth % 2 ? 'menu-item-odd' : 'menu-item-even' ],
'menu-item-depth-' . $depth
];
$depth_class_names = esc_attr[ implode[ ' ', $depth_classes ] ];
// Passed classes.
$classes = empty[ $item->classes ] ? array[] : [array] $item->classes;
$class_names = esc_attr[ implode[ ' ', apply_filters[ 'nav_menu_css_class', array_filter[ $classes ], $item ] ] ];
// Build HTML.
$output .= $indent . '
- '; // Link 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="' . esc_attr[ $item->url ] .'"' : ''; $attributes .= ' class="menu-link ' . [ $depth > 0 ? 'sub-menu-link' : 'main-menu-link' ] . '"'; // Build HTML output and pass through the proper filter. $item_output = sprintf[ '%1$s%3$s%4$s%5$s%6$s', $args->before, $attributes, $args->link_before, apply_filters[ 'the_title', $item->title, $item->ID ], $args->link_after, $args->after ]; $output .= apply_filters[ 'walker_nav_menu_start_el', $item_output, $item, $depth, $args ]; } }
Ví dụ
Chức năng walker tùy chỉnh này sẽ thêm một số lớp có điều kiện vào menu điều hướng của bạn [i. e. menu phụ, chẵn/lẻ, v.v.]
Đăng nhập để thêm phản hồiwp_nav_menu[ array[ 'menu' => 'Something custom walker', 'walker' => new WPDocs_Walker_Nav_Menu[] ] ]; /** * Custom walker class. */ class WPDocs_Walker_Nav_Menu extends Walker_Nav_Menu { /** * Starts the list before the elements are added. * * Adds classes to the unordered list sub-menus. * * @param string $output Passed by reference. Used to append additional content. * @param int $depth Depth of menu item. Used for padding. * @param array $args An array of arguments. @see wp_nav_menu[] */ function start_lvl[ &$output, $depth = 0, $args = array[] ] { // Depth-dependent classes. $indent = [ $depth > 0 ? str_repeat[ "\t", $depth ] : '' ]; // code indent $display_depth = [ $depth + 1]; // because it counts the first submenu as 0 $classes = array[ 'sub-menu', [ $display_depth % 2 ? 'menu-odd' : 'menu-even' ], [ $display_depth >=2 ? 'sub-sub-menu' : '' ], 'menu-depth-' . $display_depth ]; $class_names = implode[ ' ', $classes ]; // Build HTML for output. $output .= "\n" . $indent . '
- ' . "\n";
}
/**
* Start the element output.
*
* Adds main/sub-classes to the list items and links.
*
* @param string $output Passed by reference. Used to append additional content.
* @param object $item Menu item data object.
* @param int $depth Depth of menu item. Used for padding.
* @param array $args An array of arguments. @see wp_nav_menu[]
* @param int $id Current item ID.
*/
function start_el[ &$output, $item, $depth = 0, $args = array[], $id = 0 ] {
global $wp_query;
$indent = [ $depth > 0 ? str_repeat[ "\t", $depth ] : '' ]; // code indent
// Depth-dependent classes.
$depth_classes = array[
[ $depth == 0 ? 'main-menu-item' : 'sub-menu-item' ],
[ $depth >=2 ? 'sub-sub-menu-item' : '' ],
[ $depth % 2 ? 'menu-item-odd' : 'menu-item-even' ],
'menu-item-depth-' . $depth
];
$depth_class_names = esc_attr[ implode[ ' ', $depth_classes ] ];
// Passed classes.
$classes = empty[ $item->classes ] ? array[] : [array] $item->classes;
$class_names = esc_attr[ implode[ ' ', apply_filters[ 'nav_menu_css_class', array_filter[ $classes ], $item ] ] ];
// Build HTML.
$output .= $indent . '
- '; // Link 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="' . esc_attr[ $item->url ] .'"' : ''; $attributes .= ' class="menu-link ' . [ $depth > 0 ? 'sub-menu-link' : 'main-menu-link' ] . '"'; // Build HTML output and pass through the proper filter. $item_output = sprintf[ '%1$s%3$s%4$s%5$s%6$s', $args->before, $attributes, $args->link_before, apply_filters[ 'the_title', $item->title, $item->ID ], $args->link_after, $args->after ]; $output .= apply_filters[ 'walker_nav_menu_start_el', $item_output, $item, $depth, $args ]; } }
- 0 ? 'sub-menu-link' : 'main-menu-link' ] . '"';
// Build HTML output and pass through the proper filter.
$item_output = sprintf[ '%1$s%3$s%4$s%5$s%6$s',
$args->before,
$attributes,
$args->link_before,
apply_filters[ 'the_title', $item->title, $item->ID ],
$args->link_after,
$args->after
];
$output .= apply_filters[ 'walker_nav_menu_start_el', $item_output, $item, $depth, $args ];
}
}87 so với
85 vàwp_nav_menu[ array[ 'menu' => 'Something custom walker', 'walker' => new WPDocs_Walker_Nav_Menu[] ] ]; /** * Custom walker class. */ class WPDocs_Walker_Nav_Menu extends Walker_Nav_Menu { /** * Starts the list before the elements are added. * * Adds classes to the unordered list sub-menus. * * @param string $output Passed by reference. Used to append additional content. * @param int $depth Depth of menu item. Used for padding. * @param array $args An array of arguments. @see wp_nav_menu[] */ function start_lvl[ &$output, $depth = 0, $args = array[] ] { // Depth-dependent classes. $indent = [ $depth > 0 ? str_repeat[ "\t", $depth ] : '' ]; // code indent $display_depth = [ $depth + 1]; // because it counts the first submenu as 0 $classes = array[ 'sub-menu', [ $display_depth % 2 ? 'menu-odd' : 'menu-even' ], [ $display_depth >=2 ? 'sub-sub-menu' : '' ], 'menu-depth-' . $display_depth ]; $class_names = implode[ ' ', $classes ]; // Build HTML for output. $output .= "\n" . $indent . '
- ' . "\n";
}
/**
* Start the element output.
*
* Adds main/sub-classes to the list items and links.
*
* @param string $output Passed by reference. Used to append additional content.
* @param object $item Menu item data object.
* @param int $depth Depth of menu item. Used for padding.
* @param array $args An array of arguments. @see wp_nav_menu[]
* @param int $id Current item ID.
*/
function start_el[ &$output, $item, $depth = 0, $args = array[], $id = 0 ] {
global $wp_query;
$indent = [ $depth > 0 ? str_repeat[ "\t", $depth ] : '' ]; // code indent
// Depth-dependent classes.
$depth_classes = array[
[ $depth == 0 ? 'main-menu-item' : 'sub-menu-item' ],
[ $depth >=2 ? 'sub-sub-menu-item' : '' ],
[ $depth % 2 ? 'menu-item-odd' : 'menu-item-even' ],
'menu-item-depth-' . $depth
];
$depth_class_names = esc_attr[ implode[ ' ', $depth_classes ] ];
// Passed classes.
$classes = empty[ $item->classes ] ? array[] : [array] $item->classes;
$class_names = esc_attr[ implode[ ' ', apply_filters[ 'nav_menu_css_class', array_filter[ $classes ], $item ] ] ];
// Build HTML.
$output .= $indent . '
- '; // Link 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="' . esc_attr[ $item->url ] .'"' : ''; $attributes .= ' class="menu-link ' . [ $depth > 0 ? 'sub-menu-link' : 'main-menu-link' ] . '"'; // Build HTML output and pass through the proper filter. $item_output = sprintf[ '%1$s%3$s%4$s%5$s%6$s', $args->before, $attributes, $args->link_before, apply_filters[ 'the_title', $item->title, $item->ID ], $args->link_after, $args->after ]; $output .= apply_filters[ 'walker_nav_menu_start_el', $item_output, $item, $depth, $args ]; } }
88 so vớiwp_nav_menu[ array[ 'menu' => 'Something custom walker', 'walker' => new WPDocs_Walker_Nav_Menu[] ] ]; /** * Custom walker class. */ class WPDocs_Walker_Nav_Menu extends Walker_Nav_Menu { /** * Starts the list before the elements are added. * * Adds classes to the unordered list sub-menus. * * @param string $output Passed by reference. Used to append additional content. * @param int $depth Depth of menu item. Used for padding. * @param array $args An array of arguments. @see wp_nav_menu[] */ function start_lvl[ &$output, $depth = 0, $args = array[] ] { // Depth-dependent classes. $indent = [ $depth > 0 ? str_repeat[ "\t", $depth ] : '' ]; // code indent $display_depth = [ $depth + 1]; // because it counts the first submenu as 0 $classes = array[ 'sub-menu', [ $display_depth % 2 ? 'menu-odd' : 'menu-even' ], [ $display_depth >=2 ? 'sub-sub-menu' : '' ], 'menu-depth-' . $display_depth ]; $class_names = implode[ ' ', $classes ]; // Build HTML for output. $output .= "\n" . $indent . '
- ' . "\n";
}
/**
* Start the element output.
*
* Adds main/sub-classes to the list items and links.
*
* @param string $output Passed by reference. Used to append additional content.
* @param object $item Menu item data object.
* @param int $depth Depth of menu item. Used for padding.
* @param array $args An array of arguments. @see wp_nav_menu[]
* @param int $id Current item ID.
*/
function start_el[ &$output, $item, $depth = 0, $args = array[], $id = 0 ] {
global $wp_query;
$indent = [ $depth > 0 ? str_repeat[ "\t", $depth ] : '' ]; // code indent
// Depth-dependent classes.
$depth_classes = array[
[ $depth == 0 ? 'main-menu-item' : 'sub-menu-item' ],
[ $depth >=2 ? 'sub-sub-menu-item' : '' ],
[ $depth % 2 ? 'menu-item-odd' : 'menu-item-even' ],
'menu-item-depth-' . $depth
];
$depth_class_names = esc_attr[ implode[ ' ', $depth_classes ] ];
// Passed classes.
$classes = empty[ $item->classes ] ? array[] : [array] $item->classes;
$class_names = esc_attr[ implode[ ' ', apply_filters[ 'nav_menu_css_class', array_filter[ $classes ], $item ] ] ];
// Build HTML.
$output .= $indent . '
- '; // Link 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="' . esc_attr[ $item->url ] .'"' : ''; $attributes .= ' class="menu-link ' . [ $depth > 0 ? 'sub-menu-link' : 'main-menu-link' ] . '"'; // Build HTML output and pass through the proper filter. $item_output = sprintf[ '%1$s%3$s%4$s%5$s%6$s', $args->before, $attributes, $args->link_before, apply_filters[ 'the_title', $item->title, $item->ID ], $args->link_after, $args->after ]; $output .= apply_filters[ 'walker_nav_menu_start_el', $item_output, $item, $depth, $args ]; } }
86wp_nav_menu[ array[ 'menu' => 'Something custom walker', 'walker' => new WPDocs_Walker_Nav_Menu[] ] ]; /** * Custom walker class. */ class WPDocs_Walker_Nav_Menu extends Walker_Nav_Menu { /** * Starts the list before the elements are added. * * Adds classes to the unordered list sub-menus. * * @param string $output Passed by reference. Used to append additional content. * @param int $depth Depth of menu item. Used for padding. * @param array $args An array of arguments. @see wp_nav_menu[] */ function start_lvl[ &$output, $depth = 0, $args = array[] ] { // Depth-dependent classes. $indent = [ $depth > 0 ? str_repeat[ "\t", $depth ] : '' ]; // code indent $display_depth = [ $depth + 1]; // because it counts the first submenu as 0 $classes = array[ 'sub-menu', [ $display_depth % 2 ? 'menu-odd' : 'menu-even' ], [ $display_depth >=2 ? 'sub-sub-menu' : '' ], 'menu-depth-' . $display_depth ]; $class_names = implode[ ' ', $classes ]; // Build HTML for output. $output .= "\n" . $indent . '
- ' . "\n";
}
/**
* Start the element output.
*
* Adds main/sub-classes to the list items and links.
*
* @param string $output Passed by reference. Used to append additional content.
* @param object $item Menu item data object.
* @param int $depth Depth of menu item. Used for padding.
* @param array $args An array of arguments. @see wp_nav_menu[]
* @param int $id Current item ID.
*/
function start_el[ &$output, $item, $depth = 0, $args = array[], $id = 0 ] {
global $wp_query;
$indent = [ $depth > 0 ? str_repeat[ "\t", $depth ] : '' ]; // code indent
// Depth-dependent classes.
$depth_classes = array[
[ $depth == 0 ? 'main-menu-item' : 'sub-menu-item' ],
[ $depth >=2 ? 'sub-sub-menu-item' : '' ],
[ $depth % 2 ? 'menu-item-odd' : 'menu-item-even' ],
'menu-item-depth-' . $depth
];
$depth_class_names = esc_attr[ implode[ ' ', $depth_classes ] ];
// Passed classes.
$classes = empty[ $item->classes ] ? array[] : [array] $item->classes;
$class_names = esc_attr[ implode[ ' ', apply_filters[ 'nav_menu_css_class', array_filter[ $classes ], $item ] ] ];
// Build HTML.
$output .= $indent . '
- '; // Link 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="' . esc_attr[ $item->url ] .'"' : ''; $attributes .= ' class="menu-link ' . [ $depth > 0 ? 'sub-menu-link' : 'main-menu-link' ] . '"'; // Build HTML output and pass through the proper filter. $item_output = sprintf[ '%1$s%3$s%4$s%5$s%6$s', $args->before, $attributes, $args->link_before, apply_filters[ 'the_title', $item->title, $item->ID ], $args->link_after, $args->after ]; $output .= apply_filters[ 'walker_nav_menu_start_el', $item_output, $item, $depth, $args ]; } }
Các giá trị
87 /wp_nav_menu[ array[ 'menu' => 'Something custom walker', 'walker' => new WPDocs_Walker_Nav_Menu[] ] ]; /** * Custom walker class. */ class WPDocs_Walker_Nav_Menu extends Walker_Nav_Menu { /** * Starts the list before the elements are added. * * Adds classes to the unordered list sub-menus. * * @param string $output Passed by reference. Used to append additional content. * @param int $depth Depth of menu item. Used for padding. * @param array $args An array of arguments. @see wp_nav_menu[] */ function start_lvl[ &$output, $depth = 0, $args = array[] ] { // Depth-dependent classes. $indent = [ $depth > 0 ? str_repeat[ "\t", $depth ] : '' ]; // code indent $display_depth = [ $depth + 1]; // because it counts the first submenu as 0 $classes = array[ 'sub-menu', [ $display_depth % 2 ? 'menu-odd' : 'menu-even' ], [ $display_depth >=2 ? 'sub-sub-menu' : '' ], 'menu-depth-' . $display_depth ]; $class_names = implode[ ' ', $classes ]; // Build HTML for output. $output .= "\n" . $indent . '
- ' . "\n";
}
/**
* Start the element output.
*
* Adds main/sub-classes to the list items and links.
*
* @param string $output Passed by reference. Used to append additional content.
* @param object $item Menu item data object.
* @param int $depth Depth of menu item. Used for padding.
* @param array $args An array of arguments. @see wp_nav_menu[]
* @param int $id Current item ID.
*/
function start_el[ &$output, $item, $depth = 0, $args = array[], $id = 0 ] {
global $wp_query;
$indent = [ $depth > 0 ? str_repeat[ "\t", $depth ] : '' ]; // code indent
// Depth-dependent classes.
$depth_classes = array[
[ $depth == 0 ? 'main-menu-item' : 'sub-menu-item' ],
[ $depth >=2 ? 'sub-sub-menu-item' : '' ],
[ $depth % 2 ? 'menu-item-odd' : 'menu-item-even' ],
'menu-item-depth-' . $depth
];
$depth_class_names = esc_attr[ implode[ ' ', $depth_classes ] ];
// Passed classes.
$classes = empty[ $item->classes ] ? array[] : [array] $item->classes;
$class_names = esc_attr[ implode[ ' ', apply_filters[ 'nav_menu_css_class', array_filter[ $classes ], $item ] ] ];
// Build HTML.
$output .= $indent . '
- '; // Link 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="' . esc_attr[ $item->url ] .'"' : ''; $attributes .= ' class="menu-link ' . [ $depth > 0 ? 'sub-menu-link' : 'main-menu-link' ] . '"'; // Build HTML output and pass through the proper filter. $item_output = sprintf[ '%1$s%3$s%4$s%5$s%6$s', $args->before, $attributes, $args->link_before, apply_filters[ 'the_title', $item->title, $item->ID ], $args->link_after, $args->after ]; $output .= apply_filters[ 'walker_nav_menu_start_el', $item_output, $item, $depth, $args ]; } }
88 được xuất BÊN TRONG liên kết.wp_nav_menu[ array[ 'menu' => 'Something custom walker', 'walker' => new WPDocs_Walker_Nav_Menu[] ] ]; /** * Custom walker class. */ class WPDocs_Walker_Nav_Menu extends Walker_Nav_Menu { /** * Starts the list before the elements are added. * * Adds classes to the unordered list sub-menus. * * @param string $output Passed by reference. Used to append additional content. * @param int $depth Depth of menu item. Used for padding. * @param array $args An array of arguments. @see wp_nav_menu[] */ function start_lvl[ &$output, $depth = 0, $args = array[] ] { // Depth-dependent classes. $indent = [ $depth > 0 ? str_repeat[ "\t", $depth ] : '' ]; // code indent $display_depth = [ $depth + 1]; // because it counts the first submenu as 0 $classes = array[ 'sub-menu', [ $display_depth % 2 ? 'menu-odd' : 'menu-even' ], [ $display_depth >=2 ? 'sub-sub-menu' : '' ], 'menu-depth-' . $display_depth ]; $class_names = implode[ ' ', $classes ]; // Build HTML for output. $output .= "\n" . $indent . '
- ' . "\n";
}
/**
* Start the element output.
*
* Adds main/sub-classes to the list items and links.
*
* @param string $output Passed by reference. Used to append additional content.
* @param object $item Menu item data object.
* @param int $depth Depth of menu item. Used for padding.
* @param array $args An array of arguments. @see wp_nav_menu[]
* @param int $id Current item ID.
*/
function start_el[ &$output, $item, $depth = 0, $args = array[], $id = 0 ] {
global $wp_query;
$indent = [ $depth > 0 ? str_repeat[ "\t", $depth ] : '' ]; // code indent
// Depth-dependent classes.
$depth_classes = array[
[ $depth == 0 ? 'main-menu-item' : 'sub-menu-item' ],
[ $depth >=2 ? 'sub-sub-menu-item' : '' ],
[ $depth % 2 ? 'menu-item-odd' : 'menu-item-even' ],
'menu-item-depth-' . $depth
];
$depth_class_names = esc_attr[ implode[ ' ', $depth_classes ] ];
// Passed classes.
$classes = empty[ $item->classes ] ? array[] : [array] $item->classes;
$class_names = esc_attr[ implode[ ' ', apply_filters[ 'nav_menu_css_class', array_filter[ $classes ], $item ] ] ];
// Build HTML.
$output .= $indent . '
- '; // Link 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="' . esc_attr[ $item->url ] .'"' : ''; $attributes .= ' class="menu-link ' . [ $depth > 0 ? 'sub-menu-link' : 'main-menu-link' ] . '"'; // Build HTML output and pass through the proper filter. $item_output = sprintf[ '%1$s%3$s%4$s%5$s%6$s', $args->before, $attributes, $args->link_before, apply_filters[ 'the_title', $item->title, $item->ID ], $args->link_after, $args->after ]; $output .= apply_filters[ 'walker_nav_menu_start_el', $item_output, $item, $depth, $args ]; } }
Trong khi đó, các giá trị
85 /wp_nav_menu[ array[ 'menu' => 'Something custom walker', 'walker' => new WPDocs_Walker_Nav_Menu[] ] ]; /** * Custom walker class. */ class WPDocs_Walker_Nav_Menu extends Walker_Nav_Menu { /** * Starts the list before the elements are added. * * Adds classes to the unordered list sub-menus. * * @param string $output Passed by reference. Used to append additional content. * @param int $depth Depth of menu item. Used for padding. * @param array $args An array of arguments. @see wp_nav_menu[] */ function start_lvl[ &$output, $depth = 0, $args = array[] ] { // Depth-dependent classes. $indent = [ $depth > 0 ? str_repeat[ "\t", $depth ] : '' ]; // code indent $display_depth = [ $depth + 1]; // because it counts the first submenu as 0 $classes = array[ 'sub-menu', [ $display_depth % 2 ? 'menu-odd' : 'menu-even' ], [ $display_depth >=2 ? 'sub-sub-menu' : '' ], 'menu-depth-' . $display_depth ]; $class_names = implode[ ' ', $classes ]; // Build HTML for output. $output .= "\n" . $indent . '
- ' . "\n";
}
/**
* Start the element output.
*
* Adds main/sub-classes to the list items and links.
*
* @param string $output Passed by reference. Used to append additional content.
* @param object $item Menu item data object.
* @param int $depth Depth of menu item. Used for padding.
* @param array $args An array of arguments. @see wp_nav_menu[]
* @param int $id Current item ID.
*/
function start_el[ &$output, $item, $depth = 0, $args = array[], $id = 0 ] {
global $wp_query;
$indent = [ $depth > 0 ? str_repeat[ "\t", $depth ] : '' ]; // code indent
// Depth-dependent classes.
$depth_classes = array[
[ $depth == 0 ? 'main-menu-item' : 'sub-menu-item' ],
[ $depth >=2 ? 'sub-sub-menu-item' : '' ],
[ $depth % 2 ? 'menu-item-odd' : 'menu-item-even' ],
'menu-item-depth-' . $depth
];
$depth_class_names = esc_attr[ implode[ ' ', $depth_classes ] ];
// Passed classes.
$classes = empty[ $item->classes ] ? array[] : [array] $item->classes;
$class_names = esc_attr[ implode[ ' ', apply_filters[ 'nav_menu_css_class', array_filter[ $classes ], $item ] ] ];
// Build HTML.
$output .= $indent . '
- '; // Link 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="' . esc_attr[ $item->url ] .'"' : ''; $attributes .= ' class="menu-link ' . [ $depth > 0 ? 'sub-menu-link' : 'main-menu-link' ] . '"'; // Build HTML output and pass through the proper filter. $item_output = sprintf[ '%1$s%3$s%4$s%5$s%6$s', $args->before, $attributes, $args->link_before, apply_filters[ 'the_title', $item->title, $item->ID ], $args->link_after, $args->after ]; $output .= apply_filters[ 'walker_nav_menu_start_el', $item_output, $item, $depth, $args ]; } }
86 được xuất BÊN NGOÀI liên kết, nhưng nằm trong mục danh sách.wp_nav_menu[ array[ 'menu' => 'Something custom walker', 'walker' => new WPDocs_Walker_Nav_Menu[] ] ]; /** * Custom walker class. */ class WPDocs_Walker_Nav_Menu extends Walker_Nav_Menu { /** * Starts the list before the elements are added. * * Adds classes to the unordered list sub-menus. * * @param string $output Passed by reference. Used to append additional content. * @param int $depth Depth of menu item. Used for padding. * @param array $args An array of arguments. @see wp_nav_menu[] */ function start_lvl[ &$output, $depth = 0, $args = array[] ] { // Depth-dependent classes. $indent = [ $depth > 0 ? str_repeat[ "\t", $depth ] : '' ]; // code indent $display_depth = [ $depth + 1]; // because it counts the first submenu as 0 $classes = array[ 'sub-menu', [ $display_depth % 2 ? 'menu-odd' : 'menu-even' ], [ $display_depth >=2 ? 'sub-sub-menu' : '' ], 'menu-depth-' . $display_depth ]; $class_names = implode[ ' ', $classes ]; // Build HTML for output. $output .= "\n" . $indent . '
- ' . "\n";
}
/**
* Start the element output.
*
* Adds main/sub-classes to the list items and links.
*
* @param string $output Passed by reference. Used to append additional content.
* @param object $item Menu item data object.
* @param int $depth Depth of menu item. Used for padding.
* @param array $args An array of arguments. @see wp_nav_menu[]
* @param int $id Current item ID.
*/
function start_el[ &$output, $item, $depth = 0, $args = array[], $id = 0 ] {
global $wp_query;
$indent = [ $depth > 0 ? str_repeat[ "\t", $depth ] : '' ]; // code indent
// Depth-dependent classes.
$depth_classes = array[
[ $depth == 0 ? 'main-menu-item' : 'sub-menu-item' ],
[ $depth >=2 ? 'sub-sub-menu-item' : '' ],
[ $depth % 2 ? 'menu-item-odd' : 'menu-item-even' ],
'menu-item-depth-' . $depth
];
$depth_class_names = esc_attr[ implode[ ' ', $depth_classes ] ];
// Passed classes.
$classes = empty[ $item->classes ] ? array[] : [array] $item->classes;
$class_names = esc_attr[ implode[ ' ', apply_filters[ 'nav_menu_css_class', array_filter[ $classes ], $item ] ] ];
// Build HTML.
$output .= $indent . '
- '; // Link 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="' . esc_attr[ $item->url ] .'"' : ''; $attributes .= ' class="menu-link ' . [ $depth > 0 ? 'sub-menu-link' : 'main-menu-link' ] . '"'; // Build HTML output and pass through the proper filter. $item_output = sprintf[ '%1$s%3$s%4$s%5$s%6$s', $args->before, $attributes, $args->link_before, apply_filters[ 'the_title', $item->title, $item->ID ], $args->link_after, $args->after ]; $output .= apply_filters[ 'walker_nav_menu_start_el', $item_output, $item, $depth, $args ]; } }
Ví dụ.
Đăng nhập để thêm phản hồi
______3705 - 0 ? 'sub-menu-link' : 'main-menu-link' ] . '"';
// Build HTML output and pass through the proper filter.
$item_output = sprintf[ '%1$s%3$s%4$s%5$s%6$s',
$args->before,
$attributes,
$args->link_before,
apply_filters[ 'the_title', $item->title, $item->ID ],
$args->link_after,
$args->after
];
$output .= apply_filters[ 'walker_nav_menu_start_el', $item_output, $item, $depth, $args ];
}
}706
[html]
Đăng nhập để thêm phản hồi
php wp_nav_menu[];
[/html] - 0 ? 'sub-menu-link' : 'main-menu-link' ] . '"';
// Build HTML output and pass through the proper filter.
$item_output = sprintf[ '%1$s%3$s%4$s%5$s%6$s',
$args->before,
$attributes,
$args->link_before,
apply_filters[ 'the_title', $item->title, $item->ID ],
$args->link_after,
$args->after
];
$output .= apply_filters[ 'walker_nav_menu_start_el', $item_output, $item, $depth, $args ];
}
}706
Trong trường hợp không tìm thấy menu phù hợp với menu, có vẻ như chuyển một
2 không có thật là cách duy nhất để ngăn việc quay trở lại menu không trống đầu tiênwp_nav_menu[ array[ 'menu' => 'Something custom walker', 'walker' => new WPDocs_Walker_Nav_Menu[] ] ]; /** * Custom walker class. */ class WPDocs_Walker_Nav_Menu extends Walker_Nav_Menu { /** * Starts the list before the elements are added. * * Adds classes to the unordered list sub-menus. * * @param string $output Passed by reference. Used to append additional content. * @param int $depth Depth of menu item. Used for padding. * @param array $args An array of arguments. @see wp_nav_menu[] */ function start_lvl[ &$output, $depth = 0, $args = array[] ] { // Depth-dependent classes. $indent = [ $depth > 0 ? str_repeat[ "\t", $depth ] : '' ]; // code indent $display_depth = [ $depth + 1]; // because it counts the first submenu as 0 $classes = array[ 'sub-menu', [ $display_depth % 2 ? 'menu-odd' : 'menu-even' ], [ $display_depth >=2 ? 'sub-sub-menu' : '' ], 'menu-depth-' . $display_depth ]; $class_names = implode[ ' ', $classes ]; // Build HTML for output. $output .= "\n" . $indent . '
- ' . "\n";
}
/**
* Start the element output.
*
* Adds main/sub-classes to the list items and links.
*
* @param string $output Passed by reference. Used to append additional content.
* @param object $item Menu item data object.
* @param int $depth Depth of menu item. Used for padding.
* @param array $args An array of arguments. @see wp_nav_menu[]
* @param int $id Current item ID.
*/
function start_el[ &$output, $item, $depth = 0, $args = array[], $id = 0 ] {
global $wp_query;
$indent = [ $depth > 0 ? str_repeat[ "\t", $depth ] : '' ]; // code indent
// Depth-dependent classes.
$depth_classes = array[
[ $depth == 0 ? 'main-menu-item' : 'sub-menu-item' ],
[ $depth >=2 ? 'sub-sub-menu-item' : '' ],
[ $depth % 2 ? 'menu-item-odd' : 'menu-item-even' ],
'menu-item-depth-' . $depth
];
$depth_class_names = esc_attr[ implode[ ' ', $depth_classes ] ];
// Passed classes.
$classes = empty[ $item->classes ] ? array[] : [array] $item->classes;
$class_names = esc_attr[ implode[ ' ', apply_filters[ 'nav_menu_css_class', array_filter[ $classes ], $item ] ] ];
// Build HTML.
$output .= $indent . '
- '; // Link 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="' . esc_attr[ $item->url ] .'"' : ''; $attributes .= ' class="menu-link ' . [ $depth > 0 ? 'sub-menu-link' : 'main-menu-link' ] . '"'; // Build HTML output and pass through the proper filter. $item_output = sprintf[ '%1$s%3$s%4$s%5$s%6$s', $args->before, $attributes, $args->link_before, apply_filters[ 'the_title', $item->title, $item->ID ], $args->link_after, $args->after ]; $output .= apply_filters[ 'walker_nav_menu_start_el', $item_output, $item, $depth, $args ]; } }
1Đăng nhập để thêm phản hồi'thumbs' => [ 'srcsets' => [ 'teaser' => [ '800w' => ['width' => 800, 'quality' => 100], '1024w' => ['width' => 1024, 'quality' => 100], '1440w' => ['width' => 1440, 'quality' => 100], '2048w' => ['width' => 2048, 'quality' => 100] ] ] ]
- 0 ? 'sub-menu-link' : 'main-menu-link' ] . '"'; // Build HTML output and pass through the proper filter. $item_output = sprintf[ '%1$s%3$s%4$s%5$s%6$s', $args->before, $attributes, $args->link_before, apply_filters[ 'the_title', $item->title, $item->ID ], $args->link_after, $args->after ]; $output .= apply_filters[ 'walker_nav_menu_start_el', $item_output, $item, $depth, $args ]; } }0Đăng nhập để thêm phản hồi
- 0 ? 'sub-menu-link' : 'main-menu-link' ] . '"'; // Build HTML output and pass through the proper filter. $item_output = sprintf[ '%1$s%3$s%4$s%5$s%6$s', $args->before, $attributes, $args->link_before, apply_filters[ 'the_title', $item->title, $item->ID ], $args->link_after, $args->after ]; $output .= apply_filters[ 'walker_nav_menu_start_el', $item_output, $item, $depth, $args ]; } }0Đăng nhập để thêm phản hồi
- 0 ? 'sub-menu-link' : 'main-menu-link' ] . '"';
// Build HTML output and pass through the proper filter.
$item_output = sprintf[ '%1$s%3$s%4$s%5$s%6$s',
$args->before,
$attributes,
$args->link_before,
apply_filters[ 'the_title', $item->title, $item->ID ],
$args->link_after,
$args->after
];
$output .= apply_filters[ 'walker_nav_menu_start_el', $item_output, $item, $depth, $args ];
}
}3
Ví dụ sử dụng
709Đăng nhập để thêm phản hồiwp_nav_menu[ array[ 'menu' => 'Something custom walker', 'walker' => new WPDocs_Walker_Nav_Menu[] ] ]; /** * Custom walker class. */ class WPDocs_Walker_Nav_Menu extends Walker_Nav_Menu { /** * Starts the list before the elements are added. * * Adds classes to the unordered list sub-menus. * * @param string $output Passed by reference. Used to append additional content. * @param int $depth Depth of menu item. Used for padding. * @param array $args An array of arguments. @see wp_nav_menu[] */ function start_lvl[ &$output, $depth = 0, $args = array[] ] { // Depth-dependent classes. $indent = [ $depth > 0 ? str_repeat[ "\t", $depth ] : '' ]; // code indent $display_depth = [ $depth + 1]; // because it counts the first submenu as 0 $classes = array[ 'sub-menu', [ $display_depth % 2 ? 'menu-odd' : 'menu-even' ], [ $display_depth >=2 ? 'sub-sub-menu' : '' ], 'menu-depth-' . $display_depth ]; $class_names = implode[ ' ', $classes ]; // Build HTML for output. $output .= "\n" . $indent . '
- ' . "\n";
}
/**
* Start the element output.
*
* Adds main/sub-classes to the list items and links.
*
* @param string $output Passed by reference. Used to append additional content.
* @param object $item Menu item data object.
* @param int $depth Depth of menu item. Used for padding.
* @param array $args An array of arguments. @see wp_nav_menu[]
* @param int $id Current item ID.
*/
function start_el[ &$output, $item, $depth = 0, $args = array[], $id = 0 ] {
global $wp_query;
$indent = [ $depth > 0 ? str_repeat[ "\t", $depth ] : '' ]; // code indent
// Depth-dependent classes.
$depth_classes = array[
[ $depth == 0 ? 'main-menu-item' : 'sub-menu-item' ],
[ $depth >=2 ? 'sub-sub-menu-item' : '' ],
[ $depth % 2 ? 'menu-item-odd' : 'menu-item-even' ],
'menu-item-depth-' . $depth
];
$depth_class_names = esc_attr[ implode[ ' ', $depth_classes ] ];
// Passed classes.
$classes = empty[ $item->classes ] ? array[] : [array] $item->classes;
$class_names = esc_attr[ implode[ ' ', apply_filters[ 'nav_menu_css_class', array_filter[ $classes ], $item ] ] ];
// Build HTML.
$output .= $indent . '
- '; // Link 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="' . esc_attr[ $item->url ] .'"' : ''; $attributes .= ' class="menu-link ' . [ $depth > 0 ? 'sub-menu-link' : 'main-menu-link' ] . '"'; // Build HTML output and pass through the proper filter. $item_output = sprintf[ '%1$s%3$s%4$s%5$s%6$s', $args->before, $attributes, $args->link_before, apply_filters[ 'the_title', $item->title, $item->ID ], $args->link_after, $args->after ]; $output .= apply_filters[ 'walker_nav_menu_start_el', $item_output, $item, $depth, $args ]; } }
- 0 ? 'sub-menu-link' : 'main-menu-link' ] . '"';
// Build HTML output and pass through the proper filter.
$item_output = sprintf[ '%1$s%3$s%4$s%5$s%6$s',
$args->before,
$attributes,
$args->link_before,
apply_filters[ 'the_title', $item->title, $item->ID ],
$args->link_after,
$args->after
];
$output .= apply_filters[ 'walker_nav_menu_start_el', $item_output, $item, $depth, $args ];
}
}710 vào mục menu đang hoạt động
Đây là mã
70Đăng nhập để thêm phản hồiwp_nav_menu[ array[ 'menu' => 'Something custom walker', 'walker' => new WPDocs_Walker_Nav_Menu[] ] ]; /** * Custom walker class. */ class WPDocs_Walker_Nav_Menu extends Walker_Nav_Menu { /** * Starts the list before the elements are added. * * Adds classes to the unordered list sub-menus. * * @param string $output Passed by reference. Used to append additional content. * @param int $depth Depth of menu item. Used for padding. * @param array $args An array of arguments. @see wp_nav_menu[] */ function start_lvl[ &$output, $depth = 0, $args = array[] ] { // Depth-dependent classes. $indent = [ $depth > 0 ? str_repeat[ "\t", $depth ] : '' ]; // code indent $display_depth = [ $depth + 1]; // because it counts the first submenu as 0 $classes = array[ 'sub-menu', [ $display_depth % 2 ? 'menu-odd' : 'menu-even' ], [ $display_depth >=2 ? 'sub-sub-menu' : '' ], 'menu-depth-' . $display_depth ]; $class_names = implode[ ' ', $classes ]; // Build HTML for output. $output .= "\n" . $indent . '
- ' . "\n";
}
/**
* Start the element output.
*
* Adds main/sub-classes to the list items and links.
*
* @param string $output Passed by reference. Used to append additional content.
* @param object $item Menu item data object.
* @param int $depth Depth of menu item. Used for padding.
* @param array $args An array of arguments. @see wp_nav_menu[]
* @param int $id Current item ID.
*/
function start_el[ &$output, $item, $depth = 0, $args = array[], $id = 0 ] {
global $wp_query;
$indent = [ $depth > 0 ? str_repeat[ "\t", $depth ] : '' ]; // code indent
// Depth-dependent classes.
$depth_classes = array[
[ $depth == 0 ? 'main-menu-item' : 'sub-menu-item' ],
[ $depth >=2 ? 'sub-sub-menu-item' : '' ],
[ $depth % 2 ? 'menu-item-odd' : 'menu-item-even' ],
'menu-item-depth-' . $depth
];
$depth_class_names = esc_attr[ implode[ ' ', $depth_classes ] ];
// Passed classes.
$classes = empty[ $item->classes ] ? array[] : [array] $item->classes;
$class_names = esc_attr[ implode[ ' ', apply_filters[ 'nav_menu_css_class', array_filter[ $classes ], $item ] ] ];
// Build HTML.
$output .= $indent . '
- '; // Link 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="' . esc_attr[ $item->url ] .'"' : ''; $attributes .= ' class="menu-link ' . [ $depth > 0 ? 'sub-menu-link' : 'main-menu-link' ] . '"'; // Build HTML output and pass through the proper filter. $item_output = sprintf[ '%1$s%3$s%4$s%5$s%6$s', $args->before, $attributes, $args->link_before, apply_filters[ 'the_title', $item->title, $item->ID ], $args->link_after, $args->after ]; $output .= apply_filters[ 'walker_nav_menu_start_el', $item_output, $item, $depth, $args ]; } }
- 0 ? 'sub-menu-link' : 'main-menu-link' ] . '"';
// Build HTML output and pass through the proper filter.
$item_output = sprintf[ '%1$s%3$s%4$s%5$s%6$s',
$args->before,
$attributes,
$args->link_before,
apply_filters[ 'the_title', $item->title, $item->ID ],
$args->link_after,
$args->after
];
$output .= apply_filters[ 'walker_nav_menu_start_el', $item_output, $item, $depth, $args ];
}
}711. Các tùy chọn trên trang này hiển thị tham số này dưới dạng chuỗi để thay đổi phần tử được sử dụng để chứa menu
Tuy nhiên, ở đây không lưu ý rằng nếu bạn chuyển
712 làm giá trị, vùng chứa sẽ bị xóa hoàn toàn, chỉ để lại phần tử menuwp_nav_menu[ array[ 'menu' => 'Something custom walker', 'walker' => new WPDocs_Walker_Nav_Menu[] ] ]; /** * Custom walker class. */ class WPDocs_Walker_Nav_Menu extends Walker_Nav_Menu { /** * Starts the list before the elements are added. * * Adds classes to the unordered list sub-menus. * * @param string $output Passed by reference. Used to append additional content. * @param int $depth Depth of menu item. Used for padding. * @param array $args An array of arguments. @see wp_nav_menu[] */ function start_lvl[ &$output, $depth = 0, $args = array[] ] { // Depth-dependent classes. $indent = [ $depth > 0 ? str_repeat[ "\t", $depth ] : '' ]; // code indent $display_depth = [ $depth + 1]; // because it counts the first submenu as 0 $classes = array[ 'sub-menu', [ $display_depth % 2 ? 'menu-odd' : 'menu-even' ], [ $display_depth >=2 ? 'sub-sub-menu' : '' ], 'menu-depth-' . $display_depth ]; $class_names = implode[ ' ', $classes ]; // Build HTML for output. $output .= "\n" . $indent . '
- ' . "\n";
}
/**
* Start the element output.
*
* Adds main/sub-classes to the list items and links.
*
* @param string $output Passed by reference. Used to append additional content.
* @param object $item Menu item data object.
* @param int $depth Depth of menu item. Used for padding.
* @param array $args An array of arguments. @see wp_nav_menu[]
* @param int $id Current item ID.
*/
function start_el[ &$output, $item, $depth = 0, $args = array[], $id = 0 ] {
global $wp_query;
$indent = [ $depth > 0 ? str_repeat[ "\t", $depth ] : '' ]; // code indent
// Depth-dependent classes.
$depth_classes = array[
[ $depth == 0 ? 'main-menu-item' : 'sub-menu-item' ],
[ $depth >=2 ? 'sub-sub-menu-item' : '' ],
[ $depth % 2 ? 'menu-item-odd' : 'menu-item-even' ],
'menu-item-depth-' . $depth
];
$depth_class_names = esc_attr[ implode[ ' ', $depth_classes ] ];
// Passed classes.
$classes = empty[ $item->classes ] ? array[] : [array] $item->classes;
$class_names = esc_attr[ implode[ ' ', apply_filters[ 'nav_menu_css_class', array_filter[ $classes ], $item ] ] ];
// Build HTML.
$output .= $indent . '
- '; // Link 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="' . esc_attr[ $item->url ] .'"' : ''; $attributes .= ' class="menu-link ' . [ $depth > 0 ? 'sub-menu-link' : 'main-menu-link' ] . '"'; // Build HTML output and pass through the proper filter. $item_output = sprintf[ '%1$s%3$s%4$s%5$s%6$s', $args->before, $attributes, $args->link_before, apply_filters[ 'the_title', $item->title, $item->ID ], $args->link_after, $args->after ]; $output .= apply_filters[ 'walker_nav_menu_start_el', $item_output, $item, $depth, $args ]; } }
09wp_nav_menu[ array[ 'menu' => 'Something custom walker', 'walker' => new WPDocs_Walker_Nav_Menu[] ] ]; /** * Custom walker class. */ class WPDocs_Walker_Nav_Menu extends Walker_Nav_Menu { /** * Starts the list before the elements are added. * * Adds classes to the unordered list sub-menus. * * @param string $output Passed by reference. Used to append additional content. * @param int $depth Depth of menu item. Used for padding. * @param array $args An array of arguments. @see wp_nav_menu[] */ function start_lvl[ &$output, $depth = 0, $args = array[] ] { // Depth-dependent classes. $indent = [ $depth > 0 ? str_repeat[ "\t", $depth ] : '' ]; // code indent $display_depth = [ $depth + 1]; // because it counts the first submenu as 0 $classes = array[ 'sub-menu', [ $display_depth % 2 ? 'menu-odd' : 'menu-even' ], [ $display_depth >=2 ? 'sub-sub-menu' : '' ], 'menu-depth-' . $display_depth ]; $class_names = implode[ ' ', $classes ]; // Build HTML for output. $output .= "\n" . $indent . '
- ' . "\n";
}
/**
* Start the element output.
*
* Adds main/sub-classes to the list items and links.
*
* @param string $output Passed by reference. Used to append additional content.
* @param object $item Menu item data object.
* @param int $depth Depth of menu item. Used for padding.
* @param array $args An array of arguments. @see wp_nav_menu[]
* @param int $id Current item ID.
*/
function start_el[ &$output, $item, $depth = 0, $args = array[], $id = 0 ] {
global $wp_query;
$indent = [ $depth > 0 ? str_repeat[ "\t", $depth ] : '' ]; // code indent
// Depth-dependent classes.
$depth_classes = array[
[ $depth == 0 ? 'main-menu-item' : 'sub-menu-item' ],
[ $depth >=2 ? 'sub-sub-menu-item' : '' ],
[ $depth % 2 ? 'menu-item-odd' : 'menu-item-even' ],
'menu-item-depth-' . $depth
];
$depth_class_names = esc_attr[ implode[ ' ', $depth_classes ] ];
// Passed classes.
$classes = empty[ $item->classes ] ? array[] : [array] $item->classes;
$class_names = esc_attr[ implode[ ' ', apply_filters[ 'nav_menu_css_class', array_filter[ $classes ], $item ] ] ];
// Build HTML.
$output .= $indent . '
- '; // Link 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="' . esc_attr[ $item->url ] .'"' : ''; $attributes .= ' class="menu-link ' . [ $depth > 0 ? 'sub-menu-link' : 'main-menu-link' ] . '"'; // Build HTML output and pass through the proper filter. $item_output = sprintf[ '%1$s%3$s%4$s%5$s%6$s', $args->before, $attributes, $args->link_before, apply_filters[ 'the_title', $item->title, $item->ID ], $args->link_after, $args->after ]; $output .= apply_filters[ 'walker_nav_menu_start_el', $item_output, $item, $depth, $args ]; } }
71Đăng nhập để thêm phản hồiwp_nav_menu[ array[ 'menu' => 'Something custom walker', 'walker' => new WPDocs_Walker_Nav_Menu[] ] ]; /** * Custom walker class. */ class WPDocs_Walker_Nav_Menu extends Walker_Nav_Menu { /** * Starts the list before the elements are added. * * Adds classes to the unordered list sub-menus. * * @param string $output Passed by reference. Used to append additional content. * @param int $depth Depth of menu item. Used for padding. * @param array $args An array of arguments. @see wp_nav_menu[] */ function start_lvl[ &$output, $depth = 0, $args = array[] ] { // Depth-dependent classes. $indent = [ $depth > 0 ? str_repeat[ "\t", $depth ] : '' ]; // code indent $display_depth = [ $depth + 1]; // because it counts the first submenu as 0 $classes = array[ 'sub-menu', [ $display_depth % 2 ? 'menu-odd' : 'menu-even' ], [ $display_depth >=2 ? 'sub-sub-menu' : '' ], 'menu-depth-' . $display_depth ]; $class_names = implode[ ' ', $classes ]; // Build HTML for output. $output .= "\n" . $indent . '
- ' . "\n";
}
/**
* Start the element output.
*
* Adds main/sub-classes to the list items and links.
*
* @param string $output Passed by reference. Used to append additional content.
* @param object $item Menu item data object.
* @param int $depth Depth of menu item. Used for padding.
* @param array $args An array of arguments. @see wp_nav_menu[]
* @param int $id Current item ID.
*/
function start_el[ &$output, $item, $depth = 0, $args = array[], $id = 0 ] {
global $wp_query;
$indent = [ $depth > 0 ? str_repeat[ "\t", $depth ] : '' ]; // code indent
// Depth-dependent classes.
$depth_classes = array[
[ $depth == 0 ? 'main-menu-item' : 'sub-menu-item' ],
[ $depth >=2 ? 'sub-sub-menu-item' : '' ],
[ $depth % 2 ? 'menu-item-odd' : 'menu-item-even' ],
'menu-item-depth-' . $depth
];
$depth_class_names = esc_attr[ implode[ ' ', $depth_classes ] ];
// Passed classes.
$classes = empty[ $item->classes ] ? array[] : [array] $item->classes;
$class_names = esc_attr[ implode[ ' ', apply_filters[ 'nav_menu_css_class', array_filter[ $classes ], $item ] ] ];
// Build HTML.
$output .= $indent . '
- '; // Link 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="' . esc_attr[ $item->url ] .'"' : ''; $attributes .= ' class="menu-link ' . [ $depth > 0 ? 'sub-menu-link' : 'main-menu-link' ] . '"'; // Build HTML output and pass through the proper filter. $item_output = sprintf[ '%1$s%3$s%4$s%5$s%6$s', $args->before, $attributes, $args->link_before, apply_filters[ 'the_title', $item->title, $item->ID ], $args->link_after, $args->after ]; $output .= apply_filters[ 'walker_nav_menu_start_el', $item_output, $item, $depth, $args ]; } }
- 0 ? 'sub-menu-link' : 'main-menu-link' ] . '"'; // Build HTML output and pass through the proper filter. $item_output = sprintf[ '%1$s%3$s%4$s%5$s%6$s', $args->before, $attributes, $args->link_before, apply_filters[ 'the_title', $item->title, $item->ID ], $args->link_after, $args->after ]; $output .= apply_filters[ 'walker_nav_menu_start_el', $item_output, $item, $depth, $args ]; } }72Đăng nhập để thêm phản hồi
- 0 ? 'sub-menu-link' : 'main-menu-link' ] . '"'; // Build HTML output and pass through the proper filter. $item_output = sprintf[ '%1$s%3$s%4$s%5$s%6$s', $args->before, $attributes, $args->link_before, apply_filters[ 'the_title', $item->title, $item->ID ], $args->link_after, $args->after ]; $output .= apply_filters[ 'walker_nav_menu_start_el', $item_output, $item, $depth, $args ]; } }73Đăng nhập để thêm phản hồi
- 0 ? 'sub-menu-link' : 'main-menu-link' ] . '"'; // Build HTML output and pass through the proper filter. $item_output = sprintf[ '%1$s%3$s%4$s%5$s%6$s', $args->before, $attributes, $args->link_before, apply_filters[ 'the_title', $item->title, $item->ID ], $args->link_after, $args->after ]; $output .= apply_filters[ 'walker_nav_menu_start_el', $item_output, $item, $depth, $args ]; } }74Đăng nhập để thêm phản hồi
- 0 ? 'sub-menu-link' : 'main-menu-link' ] . '"';
// Build HTML output and pass through the proper filter.
$item_output = sprintf[ '%1$s%3$s%4$s%5$s%6$s',
$args->before,
$attributes,
$args->link_before,
apply_filters[ 'the_title', $item->title, $item->ID ],
$args->link_after,
$args->after
];
$output .= apply_filters[ 'walker_nav_menu_start_el', $item_output, $item, $depth, $args ];
}
}711 mặc định chứa menu, chỉ cần
- Sử dụng tham số ‘container’ trong mảng
- Và để trống
Giống như trong ví dụ sau
75Đăng nhập để thêm phản hồiwp_nav_menu[ array[ 'menu' => 'Something custom walker', 'walker' => new WPDocs_Walker_Nav_Menu[] ] ]; /** * Custom walker class. */ class WPDocs_Walker_Nav_Menu extends Walker_Nav_Menu { /** * Starts the list before the elements are added. * * Adds classes to the unordered list sub-menus. * * @param string $output Passed by reference. Used to append additional content. * @param int $depth Depth of menu item. Used for padding. * @param array $args An array of arguments. @see wp_nav_menu[] */ function start_lvl[ &$output, $depth = 0, $args = array[] ] { // Depth-dependent classes. $indent = [ $depth > 0 ? str_repeat[ "\t", $depth ] : '' ]; // code indent $display_depth = [ $depth + 1]; // because it counts the first submenu as 0 $classes = array[ 'sub-menu', [ $display_depth % 2 ? 'menu-odd' : 'menu-even' ], [ $display_depth >=2 ? 'sub-sub-menu' : '' ], 'menu-depth-' . $display_depth ]; $class_names = implode[ ' ', $classes ]; // Build HTML for output. $output .= "\n" . $indent . '
- ' . "\n";
}
/**
* Start the element output.
*
* Adds main/sub-classes to the list items and links.
*
* @param string $output Passed by reference. Used to append additional content.
* @param object $item Menu item data object.
* @param int $depth Depth of menu item. Used for padding.
* @param array $args An array of arguments. @see wp_nav_menu[]
* @param int $id Current item ID.
*/
function start_el[ &$output, $item, $depth = 0, $args = array[], $id = 0 ] {
global $wp_query;
$indent = [ $depth > 0 ? str_repeat[ "\t", $depth ] : '' ]; // code indent
// Depth-dependent classes.
$depth_classes = array[
[ $depth == 0 ? 'main-menu-item' : 'sub-menu-item' ],
[ $depth >=2 ? 'sub-sub-menu-item' : '' ],
[ $depth % 2 ? 'menu-item-odd' : 'menu-item-even' ],
'menu-item-depth-' . $depth
];
$depth_class_names = esc_attr[ implode[ ' ', $depth_classes ] ];
// Passed classes.
$classes = empty[ $item->classes ] ? array[] : [array] $item->classes;
$class_names = esc_attr[ implode[ ' ', apply_filters[ 'nav_menu_css_class', array_filter[ $classes ], $item ] ] ];
// Build HTML.
$output .= $indent . '
- '; // Link 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="' . esc_attr[ $item->url ] .'"' : ''; $attributes .= ' class="menu-link ' . [ $depth > 0 ? 'sub-menu-link' : 'main-menu-link' ] . '"'; // Build HTML output and pass through the proper filter. $item_output = sprintf[ '%1$s%3$s%4$s%5$s%6$s', $args->before, $attributes, $args->link_before, apply_filters[ 'the_title', $item->title, $item->ID ], $args->link_after, $args->after ]; $output .= apply_filters[ 'walker_nav_menu_start_el', $item_output, $item, $depth, $args ]; } }
- 0 ? 'sub-menu-link' : 'main-menu-link' ] . '"';
// Build HTML output and pass through the proper filter.
$item_output = sprintf[ '%1$s%3$s%4$s%5$s%6$s',
$args->before,
$attributes,
$args->link_before,
apply_filters[ 'the_title', $item->title, $item->ID ],
$args->link_after,
$args->after
];
$output .= apply_filters[ 'walker_nav_menu_start_el', $item_output, $item, $depth, $args ];
}
}18 là
711 vàwp_nav_menu[ array[ 'menu' => 'Something custom walker', 'walker' => new WPDocs_Walker_Nav_Menu[] ] ]; /** * Custom walker class. */ class WPDocs_Walker_Nav_Menu extends Walker_Nav_Menu { /** * Starts the list before the elements are added. * * Adds classes to the unordered list sub-menus. * * @param string $output Passed by reference. Used to append additional content. * @param int $depth Depth of menu item. Used for padding. * @param array $args An array of arguments. @see wp_nav_menu[] */ function start_lvl[ &$output, $depth = 0, $args = array[] ] { // Depth-dependent classes. $indent = [ $depth > 0 ? str_repeat[ "\t", $depth ] : '' ]; // code indent $display_depth = [ $depth + 1]; // because it counts the first submenu as 0 $classes = array[ 'sub-menu', [ $display_depth % 2 ? 'menu-odd' : 'menu-even' ], [ $display_depth >=2 ? 'sub-sub-menu' : '' ], 'menu-depth-' . $display_depth ]; $class_names = implode[ ' ', $classes ]; // Build HTML for output. $output .= "\n" . $indent . '
- ' . "\n";
}
/**
* Start the element output.
*
* Adds main/sub-classes to the list items and links.
*
* @param string $output Passed by reference. Used to append additional content.
* @param object $item Menu item data object.
* @param int $depth Depth of menu item. Used for padding.
* @param array $args An array of arguments. @see wp_nav_menu[]
* @param int $id Current item ID.
*/
function start_el[ &$output, $item, $depth = 0, $args = array[], $id = 0 ] {
global $wp_query;
$indent = [ $depth > 0 ? str_repeat[ "\t", $depth ] : '' ]; // code indent
// Depth-dependent classes.
$depth_classes = array[
[ $depth == 0 ? 'main-menu-item' : 'sub-menu-item' ],
[ $depth >=2 ? 'sub-sub-menu-item' : '' ],
[ $depth % 2 ? 'menu-item-odd' : 'menu-item-even' ],
'menu-item-depth-' . $depth
];
$depth_class_names = esc_attr[ implode[ ' ', $depth_classes ] ];
// Passed classes.
$classes = empty[ $item->classes ] ? array[] : [array] $item->classes;
$class_names = esc_attr[ implode[ ' ', apply_filters[ 'nav_menu_css_class', array_filter[ $classes ], $item ] ] ];
// Build HTML.
$output .= $indent . '
- '; // Link 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="' . esc_attr[ $item->url ] .'"' : ''; $attributes .= ' class="menu-link ' . [ $depth > 0 ? 'sub-menu-link' : 'main-menu-link' ] . '"'; // Build HTML output and pass through the proper filter. $item_output = sprintf[ '%1$s%3$s%4$s%5$s%6$s', $args->before, $attributes, $args->link_before, apply_filters[ 'the_title', $item->title, $item->ID ], $args->link_after, $args->after ]; $output .= apply_filters[ 'walker_nav_menu_start_el', $item_output, $item, $depth, $args ]; } }
717 nên bất kỳ giá trị nào khác sẽ khiến đối số đó không hiển thịwp_nav_menu[ array[ 'menu' => 'Something custom walker', 'walker' => new WPDocs_Walker_Nav_Menu[] ] ]; /** * Custom walker class. */ class WPDocs_Walker_Nav_Menu extends Walker_Nav_Menu { /** * Starts the list before the elements are added. * * Adds classes to the unordered list sub-menus. * * @param string $output Passed by reference. Used to append additional content. * @param int $depth Depth of menu item. Used for padding. * @param array $args An array of arguments. @see wp_nav_menu[] */ function start_lvl[ &$output, $depth = 0, $args = array[] ] { // Depth-dependent classes. $indent = [ $depth > 0 ? str_repeat[ "\t", $depth ] : '' ]; // code indent $display_depth = [ $depth + 1]; // because it counts the first submenu as 0 $classes = array[ 'sub-menu', [ $display_depth % 2 ? 'menu-odd' : 'menu-even' ], [ $display_depth >=2 ? 'sub-sub-menu' : '' ], 'menu-depth-' . $display_depth ]; $class_names = implode[ ' ', $classes ]; // Build HTML for output. $output .= "\n" . $indent . '
- ' . "\n";
}
/**
* Start the element output.
*
* Adds main/sub-classes to the list items and links.
*
* @param string $output Passed by reference. Used to append additional content.
* @param object $item Menu item data object.
* @param int $depth Depth of menu item. Used for padding.
* @param array $args An array of arguments. @see wp_nav_menu[]
* @param int $id Current item ID.
*/
function start_el[ &$output, $item, $depth = 0, $args = array[], $id = 0 ] {
global $wp_query;
$indent = [ $depth > 0 ? str_repeat[ "\t", $depth ] : '' ]; // code indent
// Depth-dependent classes.
$depth_classes = array[
[ $depth == 0 ? 'main-menu-item' : 'sub-menu-item' ],
[ $depth >=2 ? 'sub-sub-menu-item' : '' ],
[ $depth % 2 ? 'menu-item-odd' : 'menu-item-even' ],
'menu-item-depth-' . $depth
];
$depth_class_names = esc_attr[ implode[ ' ', $depth_classes ] ];
// Passed classes.
$classes = empty[ $item->classes ] ? array[] : [array] $item->classes;
$class_names = esc_attr[ implode[ ' ', apply_filters[ 'nav_menu_css_class', array_filter[ $classes ], $item ] ] ];
// Build HTML.
$output .= $indent . '
- '; // Link 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="' . esc_attr[ $item->url ] .'"' : ''; $attributes .= ' class="menu-link ' . [ $depth > 0 ? 'sub-menu-link' : 'main-menu-link' ] . '"'; // Build HTML output and pass through the proper filter. $item_output = sprintf[ '%1$s%3$s%4$s%5$s%6$s', $args->before, $attributes, $args->link_before, apply_filters[ 'the_title', $item->title, $item->ID ], $args->link_after, $args->after ]; $output .= apply_filters[ 'walker_nav_menu_start_el', $item_output, $item, $depth, $args ]; } }
- 0 ? 'sub-menu-link' : 'main-menu-link' ] . '"';
// Build HTML output and pass through the proper filter.
$item_output = sprintf[ '%1$s%3$s%4$s%5$s%6$s',
$args->before,
$attributes,
$args->link_before,
apply_filters[ 'the_title', $item->title, $item->ID ],
$args->link_after,
$args->after
];
$output .= apply_filters[ 'walker_nav_menu_start_el', $item_output, $item, $depth, $args ];
}
}89
76Đăng nhập để thêm phản hồiwp_nav_menu[ array[ 'menu' => 'Something custom walker', 'walker' => new WPDocs_Walker_Nav_Menu[] ] ]; /** * Custom walker class. */ class WPDocs_Walker_Nav_Menu extends Walker_Nav_Menu { /** * Starts the list before the elements are added. * * Adds classes to the unordered list sub-menus. * * @param string $output Passed by reference. Used to append additional content. * @param int $depth Depth of menu item. Used for padding. * @param array $args An array of arguments. @see wp_nav_menu[] */ function start_lvl[ &$output, $depth = 0, $args = array[] ] { // Depth-dependent classes. $indent = [ $depth > 0 ? str_repeat[ "\t", $depth ] : '' ]; // code indent $display_depth = [ $depth + 1]; // because it counts the first submenu as 0 $classes = array[ 'sub-menu', [ $display_depth % 2 ? 'menu-odd' : 'menu-even' ], [ $display_depth >=2 ? 'sub-sub-menu' : '' ], 'menu-depth-' . $display_depth ]; $class_names = implode[ ' ', $classes ]; // Build HTML for output. $output .= "\n" . $indent . '
- ' . "\n";
}
/**
* Start the element output.
*
* Adds main/sub-classes to the list items and links.
*
* @param string $output Passed by reference. Used to append additional content.
* @param object $item Menu item data object.
* @param int $depth Depth of menu item. Used for padding.
* @param array $args An array of arguments. @see wp_nav_menu[]
* @param int $id Current item ID.
*/
function start_el[ &$output, $item, $depth = 0, $args = array[], $id = 0 ] {
global $wp_query;
$indent = [ $depth > 0 ? str_repeat[ "\t", $depth ] : '' ]; // code indent
// Depth-dependent classes.
$depth_classes = array[
[ $depth == 0 ? 'main-menu-item' : 'sub-menu-item' ],
[ $depth >=2 ? 'sub-sub-menu-item' : '' ],
[ $depth % 2 ? 'menu-item-odd' : 'menu-item-even' ],
'menu-item-depth-' . $depth
];
$depth_class_names = esc_attr[ implode[ ' ', $depth_classes ] ];
// Passed classes.
$classes = empty[ $item->classes ] ? array[] : [array] $item->classes;
$class_names = esc_attr[ implode[ ' ', apply_filters[ 'nav_menu_css_class', array_filter[ $classes ], $item ] ] ];
// Build HTML.
$output .= $indent . '
- '; // Link 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="' . esc_attr[ $item->url ] .'"' : ''; $attributes .= ' class="menu-link ' . [ $depth > 0 ? 'sub-menu-link' : 'main-menu-link' ] . '"'; // Build HTML output and pass through the proper filter. $item_output = sprintf[ '%1$s%3$s%4$s%5$s%6$s', $args->before, $attributes, $args->link_before, apply_filters[ 'the_title', $item->title, $item->ID ], $args->link_after, $args->after ]; $output .= apply_filters[ 'walker_nav_menu_start_el', $item_output, $item, $depth, $args ]; } }
- 0 ? 'sub-menu-link' : 'main-menu-link' ] . '"';
// Build HTML output and pass through the proper filter.
$item_output = sprintf[ '%1$s%3$s%4$s%5$s%6$s',
$args->before,
$attributes,
$args->link_before,
apply_filters[ 'the_title', $item->title, $item->ID ],
$args->link_after,
$args->after
];
$output .= apply_filters[ 'walker_nav_menu_start_el', $item_output, $item, $depth, $args ];
}
}719
I. e. , đối với menu có tên 'chính', sên sẽ là '______3720'
Bạn có thể tìm thấy sên menu trong bảng cơ sở dữ liệu thuật ngữ
Đăng nhập để thêm phản hồi - 0 ? 'sub-menu-link' : 'main-menu-link' ] . '"'; // Build HTML output and pass through the proper filter. $item_output = sprintf[ '%1$s%3$s%4$s%5$s%6$s', $args->before, $attributes, $args->link_before, apply_filters[ 'the_title', $item->title, $item->ID ], $args->link_after, $args->after ]; $output .= apply_filters[ 'walker_nav_menu_start_el', $item_output, $item, $depth, $args ]; } }77Đăng nhập để thêm phản hồi
- 0 ? 'sub-menu-link' : 'main-menu-link' ] . '"';
// Build HTML output and pass through the proper filter.
$item_output = sprintf[ '%1$s%3$s%4$s%5$s%6$s',
$args->before,
$attributes,
$args->link_before,
apply_filters[ 'the_title', $item->title, $item->ID ],
$args->link_after,
$args->after
];
$output .= apply_filters[ 'walker_nav_menu_start_el', $item_output, $item, $depth, $args ];
}
}78
Sử dụng đơn giản.
Đăng nhập để thêm phản hồi
______3721 Bạn phải đăng nhập để bình chọn về mức độ hữu ích của ghi chú nàyKết quả bình chọn cho ghi chú này. -14Bạn phải đăng nhập để bình chọn về mức độ hữu ích của ghi chú này
Đóng góp bởi wp_smith —
Mình hơi bối rối về thông số “before/after” và “link_before/link_after”
Tham chiếu này cho biết “trước / sau” nằm trong liên kết, bao gồm cả văn bản liên kết và “link_before / link_after” nằm ngoài liên kết, bao gồm cả liên kết
Điều làm tôi bối rối.
Một số hướng dẫn nói rằng chính xác là ngược lại, [bên trong / bên ngoài].
tại đây và tại đây
Tôi nhận được kết quả tương tự khi xem xét một trong các cài đặt WP đang hoạt động của mình.Cái gì đúng?
Bạn đã đúng khi bối rối. các đối số trên thực tế đã được chuyển đổi về mặt tài liệu. Điều này đã được sửa trong [37561] cho 4. 6. cảm ơn vì tiền hỗ trợ. — Bởi Drew Jaynes —
Làm cách nào để kích hoạt menu trong PHP?
Bạn chỉ cần xác định menu của mình trong biến $nav và PHP sau sẽ tự động xem trang hiện tại có khớp với liên kết hay không, nếu đúng như vậy, nó sẽ thêm class="active" vào . Khi sử dụng lớp đang hoạt động, bạn có thể tạo kiểu cho liên kết đó theo cách khác, làm rõ rằng người dùng hiện đang ở trên trang đó. ?Chủ Đề