Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
64
rated 0 times [  65] [ 1]  / answers: 1 / hits: 34131  / 13 Years ago, mon, december 12, 2011, 12:00:00

So I have searched SO for an hour trying to find the answer, and also tried various methods, including this



I am trying to include my pages, along with the navigation. But on the correct page, I need the list-item to have a class of active. The navigation is in header.php and currently looks like this:



<nav>
<ul>
<li class=active> <a href=/>Home</a> </li>
<li> <a href=#>Apps</a> </li>
<li> <a href=#>Forums</a> </li>
</ul>
</nav>


First, I have no idea if JS(jQuery) would be better, or if PHP was to be better. Both wouldn't matter if it works.



Also I am a bit new with PHP and trying to learn.



What would be the easiest (hopefully) method to use? So I don't have to change a lot of code just to give a nav class=active


More From » php

 Answers
6

Asumming you have a $page variable (which contains the name of the page you are currently on):



  <nav>
<ul>
<li class=<?php echo ($page == home ? active : )?>> <a href=/>Home</a> </li>
<li class=<?php echo ($page == apps ? active : )?>> <a href=#>Apps</a> </li>
<li class=<?php echo ($page == forums ? active : )?>> <a href=#>Forums</a> </li>
</ul>
</nav>

[#88602] Saturday, December 10, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jeffery

Total Points: 180
Total Questions: 114
Total Answers: 117

Location: Chad
Member since Mon, Dec 5, 2022
1 Year ago
;