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:

  1. Open your theme's functions.php file.
  2. 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>';
    }
}
  1. Save the file.

  2. In your theme template file (e.g., header.php), replace the existing wp_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

i want wp_nav_menu without ul container

原文地址: https://www.cveoy.top/t/topic/iNEf 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录