开发者

PHP Multiple navigation highlights

开发者 https://www.devze.com 2023-02-18 12:31 出处:网络
I\'m using this piece of code below to highlight the \"active\" menu item on my global navigation. <?php

I'm using this piece of code below to highlight the "active" menu item on my global navigation.

<?php
    $path = $_SERVER['PHP_SELF'];
    $page = basename($path);
    $page = basename($path, '.php');
?>

<ul id="nav">
    <li class="home"><a href="index.php">Home</a></li>
    <li <?php if ($page == 'search') { ?>class="active"<?php } ?>><a href="#">Search</a></li>
    <li <?php if ($page == 'help') { ?>class="active"<?php } ?>><a href="help.php">Help</a></li>
</ul>

All works great but, on a couple of pages, I have a second sub menu (sidebar menu) within a global page.

I basically need to add a OR type statement into the php somehow, but I haven't a clue a how.

Example of what i mean:

    <li <?php if ($page == 'help') OR ($page == 'help-overview') OR ($page == 'help-cat-1') { ?>class="active"<?php } ?>><a href="#">Search</a&g开发者_运维知识库t;</li>

Thanks!


You could achieve that this way:

<li <?php echo ($page == 'help' || $page == 'help-overview' || $page == 'help-cat-1'  ?  'class="active"'  :  ''); ?>><a href="#">Search</a></li>


Or you could use the function in_array() which is even better in my opinion:

<li <?php echo ($page == in_array('help', 'help-overview', 'help-cat-1')  ?  'class="active"'  :  ''); ?>><a href="#">Search</a></li>


I know this is not exactly what you're asking but I would recommend you to use simple css :)

ul#nav a.active, ul#nav a:active {
   color: green;
   background: red;
}


You could use a convention to look for a base, or parent, page:

$page = explode("-", $page)[0];  # should return 'help' for all help pages

<li <?php if ($page == 'help') { ?>class="active"<?php } ?>><a href="#">Search</a></li>

This means you'll need to use the convention <parent_page>-<sub_page> when naming pages.

0

精彩评论

暂无评论...
验证码 换一张
取 消