Чтобы создать подменю в WordPress, вы можете использовать различные методы. Вот несколько часто используемых подходов и примеры кода:
Метод 1: использование встроенных функций WordPress wp_nav_menu()и wp_get_nav_menu_items():
// Register the menu location in your theme's functions.php file
function register_custom_menu() {
register_nav_menu('primary-menu', 'Primary Menu');
}
add_action('after_setup_theme', 'register_custom_menu');
// Display the menu in your theme's template file
wp_nav_menu(array(
'theme_location' => 'primary-menu',
'container' => 'nav',
'container_class' => 'sub-menu',
));
Метод 2. Использование специального класса обходчика:
class Custom_Walker_Nav_Menu extends Walker_Nav_Menu {
function start_lvl(&$output, $depth = 0, $args = array()) {
$indent = str_repeat("\t", $depth);
$output .= "\n$indent<ul class=\"sub-menu\">\n";
}
}
// Display the menu in your theme's template file
wp_nav_menu(array(
'theme_location' => 'primary-menu',
'container' => 'nav',
'container_class' => 'main-menu',
'walker' => new Custom_Walker_Nav_Menu(),
));
Метод 3. Использование плагина типа «Max Mega Menu»:
// After installing and activating the Max Mega Menu plugin, you can use the following code to display the menu in your theme's template file
if (function_exists('max_mega_menu_is_enabled') && max_mega_menu_is_enabled('primary-menu')) {
max_mega_menu('theme_location=primary-menu');
}
Это всего лишь несколько способов создания подменю в WordPress. Вы можете выбрать метод, который лучше всего соответствует вашим потребностям.