user1586214
user1586214

Reputation: 35

PHP - Highlight ONLY menu (set class "active") if any of other submenus are clicked

I´m starting in PHP, cause I´m designer, not programer. I´m having a problem when I try to set a menu highlighted if you click any of the child options. Maybe the code will be more clear than me...

<ul class="menu">
<li><a href="link-1.php" <?php if ($page == '/link-1.php') { ?>class="active"<?php } ?>>LINK 1</a></li>
<li>
    <a href="#">LINK DUMMY</a>
        <ul class="sub_menu">
            <li><a href="link-2.php">LINK 2</a></li>
            <li><a href="link-3.php">LINK 3</a></li>
            <li><a href="link-4.php">LINK 4</a></li>
            <li><a href="link-5.php">LINK 5</a></li>
        </ul>
</li>                               
    <li><a href="link-6.php" <?php if ($page == '/link-6.php') { ?>class="active"<?php } ?>>LINK 6</a></li>
    <li><a href="link-7.php" <?php if ($page == '/link-7.php') { ?>class="active"<?php } ?>>LINK 7</a></li>

</ul>

So if you hover LINK DUMMY you´ll see LINK 2, LINK 3, LINK 4, LINK 5, but what I need is to fix LINK DUMMY if you click any of it childs (LINK 2 to LINK 5) using class="active".

Any help is really appreciated.

TIA!

Upvotes: 0

Views: 477

Answers (2)

Steve
Steve

Reputation: 20469

<?php $linkOnePages=array('/link-2.php','/link-3.php','/link-4.php', '/link-5.php');?>

<ul class="menu">

    <li><a href="link-1.php" <?php if(in_array($page, $linkOnePages)) { ?>class="active"<?php } ?>>LINK 1</a></li>
    <li>  ....

Upvotes: 0

tsg
tsg

Reputation: 205

In your "Dummy Link" anchor, use:

if ($page == '/link-2.php' || $page == '/link-3.php' || $page == '/link-4.php' || $page == '/link-5.php') { 
    echo "class='active'";
}

Upvotes: 1

Related Questions