dsc56927 2016-09-11 10:18
浏览 100

如何只在某个页面上显示Wordpress二级菜单?

I have added my secondary menu on my function.php file and WordPress also allowed me to choose the secondary menu. The problem is both menu bar appeared at the same time so i have double menu bar on the top of my home page and also my about us page. What I'm try to do is show my primary menu bar only in my main home page, and show secondary menu on my about us page, services page, etc... How can i fix it?

function.php file

register_nav_menus( array(
        'primary' => esc_html__( 'Primary', 'themename' ),
        'secondary' => __( 'Secondary Menu', 'themename' ),
    ) );

Header.php file

    <?php if ( has_nav_menu( 'primary' ) 

       'theme_location'    => 'secondary',
       'container'          => 'nav',
       'container_class'    => 'navbar navbar-default',
       'menu_class'        => 'nav navbar-nav navbar-right'
; ?>
        <?php
    if ( has_nav_menu( 'secondary_navigation_menu' ) ) {
         wp_nav_menu( array( 'theme_location' => 'secondary_navigation_menu' ) );
    } ?>

        <?php if ( is_home() ): ?>
<?php if ( has_nav_menu( 'primary' ) : ?>
     //primary menu
<?php endif; ?>
<?php endif; ?>

P.S. Both of my menu bar are different CSS.

Your help will be appreciated!

  • 写回答

3条回答 默认 最新

  • doufan9377 2016-09-11 10:37
    关注

    Use the built in conditionals Wordpress provides:

    If you have set your home page to display your latest posts then you should use is_home(), if you've set your home page as the front page through Reading > Settings > Front page displays, then you should use is_front_page().

    Wrap those menu functions in a conditional statement that's applicable to your case.

    You could also combine them:

    <?php 
        if ( !(is_front_page() || is_home()) ) {
           wp_nav_menu( array( 
           'theme_location'    => 'secondary',
           'container'          => 'nav',
           'container_class'    => 'navbar navbar-default',
           'menu_class'        => 'nav navbar-nav navbar-right'
           ) ); 
        }
    
    ?>
    

    The above conditional statement will return true if the page in question isn't the home page or the front page.

    You can also use the is_page() conditional tag to check any other page.

    This function allows you to pass an optional parameter in its parenthesis to target a specific page, or number of pages using (int|string|array) Page ID, title, slug, or array.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?