dsxz84851 2019-05-07 10:46 采纳率: 100%
浏览 386

如何使用WordPress中的钩子创建菜单并将其分配到某个位置?

In my theme, I am adding a menu upon theme activation using

add_action( 'after_setup_theme', 'mycustom_register_menu' );
function mycustom_register_menu() {
    register_nav_menu( 'somelocation', __( 'My Menu') );
}

I added the location in my theme's header.php, now all that's left is to create a menu in the dashbaord and assign it to that location. It works perfectly when I do so manually.

My question is, how can I automate it? so when the theme is activated and "My Menu" is added as a location, a menu with a certain name is created and assigned to that location? Is there a hook for that?

  • 写回答

1条回答 默认 最新

  • duanguan1573 2019-05-07 11:00
    关注
    1. Creating Custom Navigation Menus in WordPress Theme

      function wpb_custom_new_menu() {
        register_nav_menu('my-custom-menu',__( 'My Custom Menu' ));
      }
      add_action( 'init', 'wpb_custom_new_menu' );
      
    2. If you want to add more than one new navigation menu location, then you would need to use a code like this:

      function wpb_custom_new_menu() {`enter code here`
        register_nav_menus(
          array(
            'my-custom-menu' => __( 'My Custom Menu' ),
            'extra-menu' => __( 'Extra Menu' )
          )
        );
      }
      add_action( 'init', 'wpb_custom_new_menu' );
      
    3. Displaying Custom Navigation Menus in WordPress Themes

      <?php
        wp_nav_menu( array( 
           'theme_location' => 'my-custom-menu', 
           'container_class' => 'custom-menu-class' ) ); 
      ?>
      
    评论

报告相同问题?

悬赏问题

  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题