1

How to add an active class on a current menu item page in html header.php . I added a custom menu in wordpress that is :

  <ul class="menuul">
            <li><a href="/#/" class="current">Начало</a></li>
            <li><a href="/#/muskulnamasa/">Действие</a></li>
            <li><a href="/#/sastavki/">Състав</a></li>
            <li><a href="/#/testosteron/">Тестостерон</a></li>
            <li><a href="/#/vuprosi/">FAQ</a></li>
            <li><a href="/#/mnenia/">Мнения</a></li>
            <li class="lastli"><a href="/#/porachka/">ПОРЪЧАЙ</a></li>
        </ul>

So how to add "active" to it? may be with some javascript? or PHP? i did try several answers here in the website: i did try this(doesnt work)

also i tried this(doesnt work)

and this(no success) ; and finally this Nothing seems to work. I am probably doing something essential wrong - please help - thanks!

PS:this didnt work too

To explain in recap what i need:

I want to add class "active" to the current menu item(current page). Not only to one of the pages, but to any active . I need something to detect the current page and add a class to it(on the

  • or on the element - whatever).
  • 5 Answers5

    1

    Just adding on current answers, this is how I did that with vanilla JavaScript:

    const currentPage = location.href;
    const allAs = document.querySelectorAll('a');
    const allAsLength = allAs.length
    
    for (let i = 0; i < allAsLength; i++) {
      if ( allAs[i].href === currentPage ) {
        allAs[i].className = "active";
      }
    }
    
    0

    Use simple Css to do that. use active class only on that li. and give the class css, like give it (Font-weight:bold; color:something;...etc). you have seprate pages for every link i guess. so when user goes to that link(that page). only that list will be have class active

    Kamran Hashmi
    • 61
    • 1
    • 7
    0

    If you use jQuery,

    • Place the following code in a common javascript file which can be accessed by all the pages.
    • Get the ending of URL and use the same as ID for respected <a> element

      url = window.location.href; //Get the current URL.
      id = url.split("/").splice(-1);
      if(id == ""){
          id = url.split("/").splice(-2); //URL ending with "/"
      }
      if(id != "" || type(id) != "undefined"){
          $(".menuul #"+id).addClass("current");
      }else{
          $(".menuul li:first-child a").addClass("current");
      }
      

    In your HTML :

    <ul class="menuul">
              <li><a href="/#/" class="current">Начало</a></li>
              <li><a id="muskulnamasa" href="/#/muskulnamasa/">Действие</a></li>
              <li><a id="sastavki" href="/#/sastavki/">Състав</a></li>
              <li><a id="testosteron" href="/#/testosteron/">Тестостерон</a></li>
              <li><a id="vuprosi" href="/#/vuprosi/">FAQ</a></li>
              <li><a id="mnenia" href="/#/mnenia/">Мнения</a></li>
              <li id="porachka" class="lastli"><a href="/#/porachka/">ПОРЪЧАЙ</a></li>
    </ul>
    

    CSS :

    Style your current CSS class accordingly.

    Nandan Bhat
    • 1,573
    • 2
    • 9
    • 21
    0

    You can achieve this with a simple loop through the result of document.querySelectorAll().
    inside the loop you have to compare the href of the element (<a> tag) with the location.href.

    Note that in the example below I have used an hard-coded string instead of location.href in the example below.

    var x = document.querySelectorAll('.menuul > li > a');
    for (var i = 0; i < x.length; i++) {
      if (x[i].href == "https://stacksnippets.net/#/") {
        x[i].className += ' active';
      }
      console.log(i + ' has the classes: ' + x[i].className);
    }
    .active {
      color: red;
    }
    <ul class="menuul">
      <li><a href="/#/" class="current">Начало</a></li>
      <li><a href="/#/muskulnamasa/">Действие</a></li>
      <li><a href="/#/sastavki/">Състав</a></li>
      <li><a href="/#/testosteron/">Тестостерон</a></li>
      <li><a href="/#/vuprosi/">FAQ</a></li>
      <li><a href="/#/mnenia/">Мнения</a></li>
      <li class="lastli"><a href="/#/porachka/">ПОРЪЧАЙ</a></li>
    </ul>
    Kevin Kloet
    • 1,086
    • 1
    • 11
    • 21
    0

    ohh - it took me 4-5 hours to find it, so ... i paste the answer here- may be some1 can use it. i putted the following code in a file menu-active-file.js and pointed for it in the footer:

     $(function () {
        var url = window.location.pathname,
    urlRegExp = new RegExp(url.replace(/\/$/, '') + "$");  
        $('a').each(function () {
                            if (urlRegExp.test(this.href.replace(/\/$/, ''))) {
                $(this).addClass('active');
            }
        });
    });
    

    Thanks for all the answers anyway.