简体   繁体   English

不同页面的WordPress自定义菜单

[英]wordpress custom menu for different page

I am trying to use a custom menu on my landingpage. 我正在尝试在目标网页上使用自定义菜单。

I used this code: 我使用以下代码:

<?php
function change_wp_nav_menu_args($args = ''){
    $pageID = get_the_id();
    if($pageID == '63') //custom menu for site with id 63
    {
        $args['menu'] = 'homepage';
    }
    return $args;
}
add_filter( 'wp_nav_menu_args', 'change_wp_nav_menu_args' );
?>

It works fine, but not only the main menu changes. 它工作正常,但不仅主菜单发生了变化。 The footer menu changes also. 页脚菜单也会更改。 But the footer menu should be the same on every page. 但是页脚菜单在每个页面上都应该相同。

How can I affect this? 我该如何影响?

This code fixed my problem: 这段代码解决了我的问题:

<?php
        add_filter( 'wp_nav_menu_args', 'bb_wp_nav_menu_args' );
            function bb_wp_nav_menu_args( $args = '' ) {
                // change the menu in the Header menu position
                if( $args['theme_location'] == 'primary' && is_page('63') ) { 
                    $args['menu'] = '6'; // 32 is the ID of the menu we want to use here
                }
                return $args;
            }
    ?>

Its great that you find your answer, but it's a suggestion to make the code dynamic rather set the menu value as static [ $args['menu']='6' ]. 很高兴找到答案,但是建议您使代码动态化,而不是将菜单值设置为静态[ $ args ['menu'] ='6' ]。

Suggestion : 建议:

Create a Meta Box [ Dropdown with list of menu ] for Page with the Label Menu. 为带有标签菜单的页面创建一个元框[带菜单列表的下拉列表]。 And use the Menu Id for the wp_nav_menu . 并使用wp_nav_menu的菜单ID。

For Dropdown [ listing Menus ] 对于下拉[列表菜单]

function your_menus()
{
    $menu_arr=NULL;
    $menus=get_terms( 'nav_menu', array( 'hide_empty' => true ) );
    $menu_arr['your-nomenu']='Default';
    foreach ( $menus as $menu ){
        $menu_arr[$menu->slug]=$menu->name;
    }
    return $menu_arr;
}//end of function

For Nav Menus : [ _your_page_menu : meta name ]. 对于导航菜单:[_your_page_menu:元名称]。 You can place the code inside a function and called it in Header or you can place this code directly into the header. 您可以将代码放置在函数中并在Header中调用它,也可以将代码直接放置在标题中。

     $page_menu_name=get_post_meta(get_the_ID(),'_your_page_menu',true)==''?'your-nomenu':get_post_meta(get_the_ID(),'_your_page_menu',true);
                if($page_menu_name==='your-nomenu')
                {
                    wp_nav_menu(array('theme_location'  => 'primary','menu_id'=> 'main-menu','container'=>false,'fallback_cb'=>'','menu_class'=>'main-navigation'));
                }
                else
                {
                    wp_nav_menu(array('menu_id'=> 'main-menu' , 'container'=>false, 'menu'=>$page_

menu_name,'fallback_cb'=>'','menu_class'=>'main-navigation'));
            }

Hope it helps you. 希望对您有帮助。

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

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