doujing6053 2014-08-24 14:53
浏览 137
已采纳

如何隐藏搜索栏以及Wordpress菜单

I have a wordpress menu in php, and everything works, however i have a multisite wordpress that has been created, if there is not menu in the Wordpress installation, i want it not to display anything, including the search, my code displays when needed, and when not needed it doesn't display anything but it still displays the search box which is in the menu. Here is my code

<?php $menuClass = 'nav';
$menuID = 'primary-navigation';
$primaryNav = '';
if (function_exists('wp_nav_menu')) {
    $primaryNav = wp_nav_menu( array( 
    'theme_location' => 'primary-nav', 
    'container' => '', 
    'fallback_cb' => '', 
    'menu_class' => $menuClass, 
    'menu_id' => $menuID, 
    'echo' => false ));
};
?>

<nav>
  <div class="navmenu">       
    <div class="wrap">
       <div id="primary-nav">
         <?php echo($primaryNav); ?>
         <div id="header-search" role="search"><form action="bloginfo("url");" method="get" id="search-form"><label><input type="text" name="s" id="site_search" placeholder="Search this site..." /></label><input type="submit" id="search-submit" value="Search" /></form></div>
       </div>
     </div>
  </div>
</nav>
  • 写回答

1条回答 默认 最新

  • dongtun2459 2014-08-24 15:35
    关注

    EDIT:

    Ok, you need to first look if the nav menu exist

            <?php
            $primaryNav = '';
            if ( has_nav_menu( 'primary-nav' ) ) {
                $primaryNav = wp_nav_menu( array( 
                'theme_location' => 'primary-nav', 
                'container' => '', 
                'fallback_cb' => '', 
                'menu_class' => 'primary-navigation', 
                'menu_id' => 'nav', 
                'echo' => false ));
            }
    
            $search_out = '<div id="header-search" role="search">
                    <form action="bloginfo("url");" method="get" id="search-form">
                        <label><input type="text" name="s" id="site_search" placeholder="Search this site..." /></label>
                        <input type="submit" id="search-submit" value="Search" />
                    </form>
                </div>';
    
            ?>
    
    <nav>
      <div class="navmenu">       
        <div class="wrap">
           <div id="primary-nav">
             <?php ($primaryNav!='') ? _e($primaryNav . $search_out) : '';
             ?>
           </div>
         </div>
      </div>
    </nav>
    

    First you check if there is a nav menu in 'primary-nav' location, and if there is, you print it out. Before that, it's empty. And then you check if it's not empty (!=''). If that condition is true it will echo it out, along with search, if not, it won't. You could probably make an if clause that puts the whole <nav> section appear if there is $primaryNav.

    EDIT2:

    Your old code might also work, just check that it's not empty, with the code above.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?
  • ¥15 讲解电路图,付费求解