Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8033379
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T01:40:46+00:00 2026-06-05T01:40:46+00:00

I’m trying get the code below to always highlight the current page of a

  • 0

I’m trying get the code below to always highlight the current page of a theme that I’m working on but it does nothing. The HTML part is as shown below the script. Any suggestions or solution for the fix.

<script type="text/javascript">
    var url = window.location.href;
    url = url.substr(url.lastIndexOf("/") + 1);
    $("#navbar").find("a[href='" + url + "']").addClass("current");
</script>

<div id="navbar">
    <ul>
        <li><a href="index.php">home</a></li>
        <li><a href="services/">services</a></li>
        <li><a href="procedure/">procedure</a></li>
        <li><a href="about-us/">about</a></li>
        <li><a href="contact-us/">contact</a></li>
    </ul>
</div>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T01:40:47+00:00Added an answer on June 5, 2026 at 1:40 am

    You need to look into WP Nav Menu.

    I’m guessing this is a theme you’re developing? If so, Register a Nav Menu in your functions.php file. What this will do is allow you to define your menu in the WordPress dashboard (Appearance->Menus), and customize it however you like.

    The reason why I’m suggesting this is that not only will it allow you to have total control over your Menu in a very intuitive way, but it also defines all the classes you could possibly need for your stylesheet automatically (including current items, and ancestry).

    That is the cleanest option you have at your disposal. You can also look into WP List Pages for a similar (albeit, more limited) solution.

    Otherwise, you’ll have to do something dirty like this:

    <?php
    $id = get_the_ID();
    $class = ' class="current"';
    ?>
    <ul>
        <li<?php echo is_home() ? $class : ''; ?>><a href="<?php echo home_url(); ?>">home</a></li>
        <li<?php echo $id==$serives_ID ? $class : ''; ?>><a href="<?php echo get_permalink($serives_ID); ?>">services</a></li>
        <li<?php echo $id==$procedure_ID ? $class : ''; ?>><a href="<?php echo get_permalink($procedure_ID ); ?>">procedure</a></li>
        <li<?php echo $id==$about_ID ? $class : ''; ?>><a href="<?php echo get_permalink($about_ID); ?>">about</a></li>
        <li<?php echo $id==$contact_ID ? $class : ''; ?>><a href="<?php echo get_permalink($contact_ID); ?>">contact</a></li>
    </ul>
    

    Replace $services_ID, $procedure_ID, etc. with their respective Page IDs, and if the current ID matches any one of them, that item will have a class of “current” applied to it, which you can then target in your stylesheet.

    The main reason why I suggest a server-side solution over using any kind of jQuery is that this is not a task that is suited specifically for jQuery itself.

    While the jQuery library is a powerful tool, WordPress has all of the built-in functionality you need to detect everything you want to detect in this instance. Let the server do as much work as possible before letting the client’s browser take over. Otherwise, you’ll be looking at some serious load times.

    UPDATE:
    Based on your last comment, if you’re going to go with the messy way and you don’t want to be bothered with finding IDs of your pages, you can also do this:

    <?php
    $slugs = array('services', 'procedure', 'about-us', 'contact-us');
    ?>
    <ul>
        <li<?php echo is_home() ? ' class="current"' : ''; ?>><a href="<?php echo home_url(); ?>">home</a></li>
        <?php
        foreach($slugs as $slug)
        {
            $page = get_page_by_path($slug);
            $class = is_page($slug) ? ' class="current"' : '';
            echo '<li'.$class.'><a href="'.get_permalink($page->ID).'">'.str_replace('-us','',$slug).'</a></li>';
        }
        ?>
    </ul>
    

    UPDATE BASED ON LAST COMMENT:

    Now that you’re using wp_nav_menu, you can target the current menu item with CSS. Here’s an example that will turn the background of the current item to red:

    <style type="text/css">
    .current-menu-item{background:#F00;}
    </style>
    

    Apply these rules either in your header, or preferably in your main style.css file. Good luck!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to create an if statement in PHP that prevents a single post
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.