简体   繁体   English

在自定义主题WordPress上添加下拉菜单

[英]Adding dropdown menu on custom theme wordpress

I am trying to create a dropdown menu for a theme I am developing but the sub menu items are showing up in the alongside the parent menu items. 我正在尝试为正在开发的主题创建下拉菜单,但是子菜单项显示在父菜单项旁边。

I have it saved as a submenu item 我将其保存为子菜单项

在此处输入图片说明

Here is what it looks like our programs should be under about us now it's just all jumbled 这就是我们的程序应该在我们周围的样子,现在简直是混乱

在此处输入图片说明

This is what I have for my navigation in functions.php 这就是我在functions.php中导航的内容

function register_my_menus() {
  register_nav_menus(
    array(
      'header-menu' => __( 'Header Menu' ),
      'extra-menu' => __( 'Extra Menu' )
    )
  );
}

add_action( 'init', 'register_my_menus' );

   $defaults = array(
    'default-image'          => '',
    'width'                  => 0,
    'height'                 => 0,
    'flex-height'            => false,
    'flex-width'             => false,
    'uploads'                => false,
    'random-default'         => false,
    'header-text'            => true,
    'default-text-color'     => '',
    'wp-head-callback'       => '',
    'admin-head-callback'    => '',
    'admin-preview-callback' => '',
);
add_theme_support( 'custom-header', $defaults );

and in my header.php 并在我的header.php中

<div id="menu">
    <ul>
        <li id="access"><?php wp_nav_menu( array( 'sort_column' => 'menu_order', 'container_class' => 'menu-header' ) ); ?></li>
    </ul>
</div>

I can't seem to find anything online. 我似乎无法在网上找到任何东西。 Any help would be appreciated! 任何帮助,将不胜感激!

You have to create class for menu and you have to apply those class to menu. 您必须为菜单创建类,并且必须将这些类应用于菜单。 You can use walker to add several conditional classes to your menu. 您可以使用Walker将几个条件类添加到菜单中。

For more details you can refer to 有关更多详细信息,请参阅

https://developer.wordpress.org/reference/functions/wp_nav_menu/ https://developer.wordpress.org/reference/functions/wp_nav_menu/

wp_nav_menu( array( 
  'sort_column' => 'menu_order', 
  'container_class' => 'menu-header',
  'menu_class' => 'your_class' //you can add your class here,
  'container' => 'div',
  'menu' => 'main-nav',
  'theme_location'    => 'my-header-menu', // Select the menu name registered in functions.php
  'walker'            => "", // Instance of a custom walker class to add conditional classes into your nav menu
));

You need to add different classes to menu. 您需要向菜单添加不同的类。 and apply appropriate CSS for position( left: (n)px; top: (n)px; ) to those classes. 并将适当的CSS应用于position( left: (n)px; top: (n)px; )这些类。

wp_nav_menu( array( 
  'sort_column' => 'menu_order', 
  'container_class' => 'menu-header',
  'menu_class' => 'custom_menu' //add class,
  'container' => 'div',
  'menu' => 'main-nav',
));

Hope it will help you :) 希望它能对您有所帮助:)

Add this code in functions.php file 将此代码添加到functions.php文件中

add_action('wp_enqueue_scripts', 'buena_child_scripts');

function register_flatlearn_menu(){
  //register menu
  register_nav_menus(
    array(
      'primary-menu' => __('Primary Menu'),
      'footer-menu' => __('Footer Menu')
      )
    );
  }
  //attach with action hook
  add_action("init","register_flatlearn_menu");

after this code, you add the following code in header.php file as given below: 在此代码之后,将以下代码添加到header.php文件中,如下所示:

<nav>
  <?php
     wp_nav_menu(array(
       'sort_column' => 'menu_order',
       'menu-id' =>'primary-menu',
       'depth' => 0,
       'container' =>'false' ,
       'menu_class' => 'nav topnav',
     ));
  ?>
</nav>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM