0

I am trying to create a menu where a class "active" is assigned to the page whenever it's selected and loaded. Right now it is applied only to the index page by default.

My menu snippet:

<ul class="nav navbar-nav">
     <li class="active"><a href="http://localhost/wp/index.php">Main</a></li>
     <li><a href="http://localhost/wp/news">News</a></li>
     <li><a href="http://localhost/wp/contacts">Contacts</a></li>
</ul>
Jacob van Lingen
  • 8,989
  • 7
  • 48
  • 78
Azim
  • 51
  • 1
  • 11
  • But he has to remove existing class on other li also – Zeeshan Hyder Jan 29 '16 at 06:17
  • @OP, are links being formed in some sort of loops ? – Rayon Jan 29 '16 at 06:18
  • Are links being formed from the db? i.e. are you forming them on server side? – progrAmmar Jan 29 '16 at 06:20
  • @TariqHusain yes, they are in php, using a Wordpress theme template – Azim Jan 29 '16 at 06:25
  • @Tristan yes I am using Wordpress. All of my pages use the same template. – Azim Jan 29 '16 at 06:25
  • @ZeeshanHyder Sorry if it was unclear. I want to add "active" class to my li, yes, depending on the selected and loaded page. – Azim Jan 29 '16 at 06:26
  • @RayonDabre they aren't as far as I'm concerned. – Azim Jan 29 '16 at 06:27
  • @Azim :see my answer .. with demo – Amar Singh Jan 29 '16 at 06:34
  • @Azim : see my updated answer. – Ravi Hirani Jan 29 '16 at 07:03