2013-02-12 6 views
0

내 탐색을 위해 PHP 포함이 있습니다. 현재 메뉴 링크가 표시되어 있는지 확인하여 클래스가 생겼습니다. current-menu-item. 모든PHP 포함, 드롭 다운 탐색 선택한 문제

각 페이지는 <?php $current_page = "pageName"; ?>

을 가지고 그리고 난 당신이 하위의에있을 때 선택으로 표시 할 주요 <li><li class="<?php if($current_page == 'dev' || 'seo' || 'mobile' || 'social' || 'hosting') echo "current-menu-item";?>">

이 ... 링크 드롭 다운을 제외하고 잘 작동 링크 ...하지만이 방법을 시도했을 때 내 사이트의 모든 페이지에서 선택된 것으로 표시되었습니다. ...

참조가 필요하면 내 사이트가 thwebco.com입니다. 여기

if

<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

귀하의 사용이 잘못 ... 코드입니다. $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') 

는 필요 INVALID 아니지만, 당신은 그것을 사용하는 잘못된. ||은 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);