Class active in the include menu

1

In my pages PHP I use a include to call a file that contains my menu:

<?php include("menu.php"); ?>

Menu file:

<li class="current-menu-item"><a href="#">Hosting</a>
<ul>
   <li><a href="hosting-shared.php">SHARED HOSTING</a></li>
   <li><a href="free-hosting.php">FREE HOSTING</a></li>
   <li><a href="cloud-hosting.php">CLOUD HOSTING</a></li>
   <li><a href="reseller-hosting.php">RESELLER HOSTING</a></li>                
</ul>
</li>
   <li><a href="domains.php">DOMAINS</a></li>
   <li><a href="#">Servers</a>
<ul>
   <li><a href="cloud-vps.php">CLOUD VPS</a></li>
   <li><a href="dedicated-servers.php">DEDICATED SERVERS</a></li>
   <li><a href="order-slider.php">ORDER SLIDER</a></li>

When the menu is active you get this class

class="current-menu-item"             

How do I enter the class active only in the menu accessed?

    
asked by anonymous 28.01.2017 / 02:38

2 answers

1

Make the menu.php like this:

<?php
    /**
    * Verifica o script ativo e compara com $script indicado;
    * Se a comparação for positiva, imprime a CLASS;
    * Se a comparação falhar, retorna FALSE.
    * @return string|boolean
    */
    function setClass($script = NULL){
        $ativo = str_replace('/','',$_SERVER['PHP_SELF']);
        if($ativo == $script){
            echo 'class="current-menu-item"';
        }
        return FALSE;
    }
?>
<li><a <?=setClass('testing.php')?> href="testing.php">Hosting</a>
         <ul>
            <li><a <?=setClass('hosting-shared.php')?> href="hosting-shared.php">SHARED HOSTING</a></li>
            <li><a <?=setClass('free-hosting.php')?> href="free-hosting.php">FREE HOSTING</a></li>
            <li><a <?=setClass('cloud-hosting.php')?> href="cloud-hosting.php">CLOUD HOSTING</a></li>
            <li><a <?=setClass('reseller-hosting.php')?> href="reseller-hosting.php">RESELLER HOSTING</a></li>
         </ul>
</li>
<li><a <?=setClass('domains.php')?> href="domains.php">DOMAINS</a></li>
     <li><a href="#">Servers</a>
         <ul>
            <li><a <?=setClass('cloud-vps.php')?> href="cloud-vps.php">CLOUD VPS</a></li>
            <li><a <?=setClass('dedicated-servers.php')?> href="dedicated-servers.php">DEDICATED SERVERS</a></li>
            <li><a <?=setClass('order-slider.php')?> href="order-slider.php">ORDER SLIDER</a></li>
        </ul>
    </li>
    
28.01.2017 / 13:39
1
  

This is another non-PHP option!

Once it's something visual I think it can be handled on the client side by using Javascript / Jquery.

For example:

var pagina = window.location.pathname.split('/')[ window.location.pathname.split('/').length - 1 ];

$('.menu').find('a[href="'+pagina+'"]').closest('li').addClass('current-menu-item');

Assuming this:

<div class="menu">
    <li><a href="#">Hosting</a>
    <ul>
       <li><a href="hosting-shared.php">SHARED HOSTING</a></li>
       <li><a href="free-hosting.php">FREE HOSTING</a></li>
       <li><a href="cloud-hosting.php">CLOUD HOSTING</a></li>
       <li><a href="reseller-hosting.php">RESELLER HOSTING</a></li>                
    </ul>
</div>
The JQuery will search for the a that has href equal to the page the user is accessing, then it will find the li closer and then add the current-menu-item class on it.

Basically it will work like this:

  • Find any element of class menu
  • Find a with href equal to pagina
  • Find li closer
  • Add class current-menu-item
  • 29.01.2017 / 15:29