How to set active page class in a header included on all pages?

Asked

Viewed 253 times

0

My site has a header that is called on all internal pages with the function include php. It turns out I would like to have the class="active" on the page that is active. How to do this if I only have one header for all internal pages? I am using bootstrap 3.

  • 1

    Please add a sample code from your page and specify how the navigation between pages of your site works, in order to generate a response focused on your problem, otherwise the answer can be very comprehensive.

1 answer

2

One approach would be to use the following on your pages:

$menuAtivo = 'identificador_menu_ativo'; // o identificador deve ser modificado de acordo com a página (inicio, cadastro, listagem por exemplo)
include 'cabecalho.php';

You can use the above code on all pages by inserting a variable to identify the menu item before including the header. Inside the.php header, you can use something like this:

<ul>
    <li <?php echo $menuAtivo == 'inicio' ? 'class="ativo"' : '' ?> > Início </li>
    <li <?php echo $menuAtivo == 'cadastro' ? 'class="ativo"' : '' ?> > Cadastro </li>
    <li <?php echo $menuAtivo == 'listagem' ? 'class="ativo"' : '' ?> > Listagem </li>
</ul>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.