2013-02-12 20 views
0

我有一個PHP包含我的導航。我想確保顯示當前菜單鏈接,所以我有一個類current-menu-item。所有作品除了鏈接下拉精...PHP包含,下拉導航選擇問題

每個頁面都有一個<?php $current_page = "pageName"; ?>

,我有<li class="<?php if($current_page == 'dev' || 'seo' || 'mobile' || 'social' || 'hosting') echo "current-menu-item";?>">

在主<li>作爲選擇,當你在任何一個子的,將顯示鏈接...但是當我嘗試這種方式,它顯示爲在我的網站上的每一頁上選擇...

我的網站是thwebco.com,如果你需要一個參考。

這裏是代碼...

<ul id="nav" class="sf-menu"> 
         <li class="<?php if($current_page == 'home') echo "current-menu-item";?>"><a href="index.php">Home<span class="subheader">Welcome</span></a></li> 
         <li class="<?php if($current_page == 'development' || 'seo' || 'mobile' || 'social' || 'hosting') echo "current-menu-item";?>"><a href="#">Services<span class="subheader">What we offer</span></a> 
          <ul> 
           <li><a href="development.php"><span> Web Design & Development</span></a></li> 
           <li><a href="seo.php"><span> Search Engine Optimization </span></a></li> 
           <li><a href="mobile.php"><span> Mobile </span></a></li> 
           <li><a href="social.php"><span> Social Media </span></a></li> 
           <li><a href="hosting.php"><span> Web Hosting & Email </span></a></li> 
          </ul> 
         </li> 

         <li><a href="#">Our Work <span class="subheader">See our work</span></a> 
         <ul> 
           <li class="<?php if($current_page == 'portfolio') echo "current-menu-item";?>"><a href="portfolio.php"><span>Portfolio </span></a></li> 
           <li class="<?php if($current_page == 'case') echo "current-menu-item";?>"><a href="case.php"><span>Case Studies </span></a></li> 
           </ul> 
           </li> 
         <li class="<?php if($current_page == 'about') echo "current-menu-item";?>"><a href="about.php">About Us<span class="subheader">Who we are</span></a> 

         </li> 
         <li class="<?php if($current_page == 'contact') echo "current-menu-item";?>"><a href="contact.php">Contact<span class="subheader">Get in touch</span></a></li> 
         <li class="<?php if($current_page == 'quote') echo "current-menu-item";?>"><a href="quote.php">Get A Quote<span class="subheader">Let us help</span></a></li> 
        </ul> 

回答

0

你的if使用是不正確。如果$current_page=='dev'seo以及非空字符串總是計算爲true,則它的計算結果爲true。

它應該是:

<?php if ($current_page == 'dev' || $current_page == 'seo' || $current_page == 'mobile' || $current_page == 'social' || $current_page == 'hosting') echo "current-menu-item"; ?> 

或者更簡單:

<?php if (in_array($current_page, array('dev', 'seo', 'mobile', 'social', 'hosting')) echo "current-menu-item"; ?> 
0

這個if語句:

if($current_page == 'development' || 'seo' || 'mobile' || 'social' || 'hosting') 

是沒有必要無效的,因爲這樣,但你正在使用它錯誤。 ||意味着OR,所以它會分裂你的狀況。 這就是正在處理你目前的狀況:

if(
    $current_page == 'development' || 
    'seo' == true; || 
    'mobile' == true; || 
    'social' == true; || 
    'hosting' == true; 
) 

等等,當然,由於搜索引擎優化,移動,社交和託管不等於false時總是如此。

開展你正在試圖做的是創建一個可接受值的數組,然後覈對該值,使用in_array(),像這樣的最好方法:

$pages = Array("development","seo","mobile","social","hosting"); 
if(in_array($current_page,$pages);