dsgwii4867 2014-09-26 09:44
浏览 30

如何以编程方式更改Wordpress中的菜单?

I've created one menu in Norwegian menu_no and one in English menu_en.

enter image description here

I can see that my theme supports only one menu, but I'm not planning on using more that one menu at a time. So when user selects English language, how can I change the active menu?

I've not found anything on Google and I can't find the right function in nav-menu.php.

UPDATE
I found quite a simple solution for my problem. I just had to think a bit differently. In my functions.phpI added this code:

add_action('init', 'register_menus');

function register_menus(){
    register_nav_menus( array(
        'menu_no' => 'Norwegian menu',
        'menu_en' => 'English menu',
    ) );
}

and in my header.php file I use this code:

global $lang;

$args = array(
    'theme_location' => 'menu_'.$lang,
    'container'         => false
);


<?php wp_nav_menu($args); ?>

Voila. I'll post it as answer later - if not anyone else comes up with a better idea.

  • 写回答

1条回答 默认 最新

  • dongzhonggua4229 2014-09-26 17:22
    关注

    Here is How You Can Do That.

    Past this code to function.php file

    // Register Navigation Menus
    function custom_navigation_menus() {
    $locations = array(
        'first_menu' => __( 'Menu for English language', 'text_domain' ),
        'second_menu' => __( 'Menu for Other Language', 'text_domain' ),
    );
    register_nav_menus( $locations );
    
    }
    
    // Hook into the 'init' action
    
    add_action( 'init', 'custom_navigation_menus' );
    

    You can customize the above code. Now use wp_nav_menu function to display anywhere you want like this

                                  <?php
                                 wp_nav_menu( array(
                                    'menu'              => 'primary',
                                    'theme_location'    => 'first_menu',
                                );
    
                                 ?>
    
                                <?php
                                 wp_nav_menu( array(
                                    'menu'              => 'secondary',
                                    'theme_location'    => 'second_menu',
                                );
    
                                 ?>
    

    You can display each menu in different places. Now you to Option in you menu Primary and Secondary

    评论

报告相同问题?

悬赏问题

  • ¥20 关于php中URL传递GET全局变量的问题
  • ¥15 怎么改成循环输入删除(语言-c语言)
  • ¥15 安卓C读取/dev/fastpipe屏幕像素数据
  • ¥15 pyqt5tools安装失败
  • ¥15 mmdetection
  • ¥15 nginx代理报502的错误
  • ¥100 当AWR1843发送完设置的固定帧后,如何使其再发送第一次的帧
  • ¥15 图示五个参数的模型校正是用什么方法做出来的。如何建立其他模型
  • ¥100 描述一下元器件的基本功能,pcba板的基本原理
  • ¥15 STM32无法向设备写入固件