Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

192
Visualizações
Highlight current item is not working in wordpress pagination (js issue?)

there.

I'm almost finishing a website I'm developing from scratch in wordpress, without any premade plugins or theme. But I'm facing a silly issue, I guess, that I don't understand what is going on.

There is this kind of menu item that I need to hightlight. It works fine everywhere but in pagination. I'm not using a wordpress menu, so I can't use prebuilt classes like "i.current_page_item" and "li.current_page_item a"

That's why I'm using this following JS:

const activePage = window.location.href;
const navLinks = document.querySelectorAll("nav .custom-menu-class ul li a");

navLinks.forEach((mylink) => {
  if (mylink.href.includes(`${activePage}`)) {
    mylink.classList.add("active-menu");
  }
});

It works in "http://localhost/mysite/pizza", but it doesnt work in "http://localhost/mysite/pizza/page/2"

Here it goes my menu code in php:

    <nav class="menu">
        <div class="custom-menu-class">
            <ul>
                <a href="<?php echo get_page_link(724);?>"><li><?php echo get_the_title(724);?></li></a>
                <a href="<?php echo get_page_link(726);?>"><li><?php echo get_the_title(726);?></li></a>
                <a href="<?php echo get_page_link(724);?>"><li><?php echo get_the_title(728);?></li></a>
            </ul>
            </div>

Does anybody know what am I doing wrong? Thanks a lot for your attention.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

When I examine your code, the first problem I see is in here:

const navLinks = document.querySelectorAll("nav .custom-menu-class ul li a");

Since you have defined the hierarchy as

nav > .custom-menu-class > ul > a > li

you cant get the related menu items in your Javascript with

"nav .custom-menu-class ul li a"

to get rid of this problem you can simply remove the "li" from it and write it down like:

"nav .custom-menu-class ul a"

Other than that, to see what is going on while matching your page urls with "window.location.href", you can simply output the variables and compare them. To do this, once you create your activePage variable print it to your console with

console.log(activePage);

and check weather it matches with your page links.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda