如何在wordpress中的不同页面上设置不同的菜单?

时间:2014-11-27 10:21:46

标签: wordpress

我正在尝试设置不同页面的不同菜单,如。

在主页中我需要像这样的主菜单链接

<a href='#home'>Home</a>
<a href='http://example.com/product'>Product</a>
<a href='#services'>Services</a>
<a href='#conact'>Contact Us</a> 

在产品页面

<a href='http://example.com/#home'>Home</a>
<a href='http://example.com/product'>Product</a>
<a href='http://example.com/#services'>Services</a>
<a href='http://example.com/#conact'>Contact Us</a>  

我正在使用一个页面主题,所以请帮助我进行逻辑开发。 感谢

1 个答案:

答案 0 :(得分:2)

如果不看更多代码就不可能回答这个问题,但从抽象意义上说,这可以很容易地实现。正如Yatendra指出的那样,你需要在functions.php文件中注册两个菜单:

function register_my_menus() {
  register_nav_menus(
    array(
      'home-menu' => __( 'Header Menu' ),
      'product-menu' => __( 'Product Menu' )
    )
  );
}
add_action( 'init', 'register_my_menus' );

然后,您需要在header.php中嵌入类似的内容,导航应该使用is_page_template()wp_nav_menu()函数。

<?php 

    if (is_page_template( 'products.php' )) // change this to the name of the file
    {
         // load the product-menu
         wp_nav_menu( array( 'theme_location' => 'product-menu' ) );         
    }
    else
    {
         // load the header-menu
         wp_nav_menu( array( 'theme_location' => 'header-menu' ) ); 
    }
?>

但是,对于单页主题,类似于视差主题,最好不要放入条件并将它们包装在<div>标签中,并使用jQuery显示和隐藏它们。但是,我们真的需要看到更多代码。

食典参考文献:

  1. is_page_template()
  2. Registering a Menu
相关问题