i want wp_nav_menu without ul container
To display the WordPress navigation menu without the <ul> container, you can use the wp_nav_menu() function with a custom walker class. Here's an example:
- Open your theme's
functions.phpfile. - Add the following code:
class Custom_Walker_Nav_Menu extends Walker_Nav_Menu {
public function start_lvl(&$output, $depth = 0, $args = array()) {
// Remove the opening <ul> tag
}
public function end_lvl(&$output, $depth = 0, $args = array()) {
// Remove the closing </ul> tag
}
public function start_el(&$output, $item, $depth = 0, $args = array(), $id = 0) {
$output .= '<li>';
// Add your custom code for each menu item
}
public function end_el(&$output, $item, $depth = 0, $args = array()) {
$output .= '</li>';
}
}
-
Save the file.
-
In your theme template file (e.g.,
header.php), replace the existingwp_nav_menu()function with the following code:
<?php
wp_nav_menu(array(
'theme_location' => 'primary',
'menu_class' => 'menu',
'walker' => new Custom_Walker_Nav_Menu()
));
?>
Make sure to replace 'primary' with the actual menu location you want to display.
With this setup, the <ul> container will be removed, and each menu item will be wrapped in a <li> element
原文地址: https://www.cveoy.top/t/topic/iNEf 著作权归作者所有。请勿转载和采集!