PHP包含,下拉导航选择的问题


PHP Include, dropdown nav selected issue

我有一个用于导航的PHP包。我想确保显示当前的菜单链接,这样我就有了一个类current-menu-item。除了下拉链接外,所有操作都很好。。。

每个页面都有一个<?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>

您对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"; ?>

This-if语句:

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

这不是必要的INVALID,但你用错了。||的意思是OR,所以它从本质上拆分了您的条件这就是您当前状况的处理方式:

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

所以,当然,由于seo、移动、社交和托管不等于false,所以它总是true。

执行您尝试执行的操作的最佳方法是创建一个具有可接受值的数组,然后使用in_array()检查该值,如下所示:

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