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

  • SEARCH
  • Home
  • 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 8238781
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T19:55:48+00:00 2026-06-07T19:55:48+00:00

I have a jQuery way of adding a .current class to an anchor link

  • 0

I have a jQuery way of adding a .current class to an anchor link based on the site url and the corresponding page (i.e., if I’m on the “about” page, it’ll add a .current class to the anchor link in the nav menu that directs to “about.php”). It definitely does the job, and it’s super simple, but I wanted a PHP way to accomplish this, in a similar fashion that WordPress does. What I ended up with is something that is sort of unconventional and not easily modifiable (code below)

My question is: Is there an easier way of doing this? You’ll see in my code that I have to assign a variable $nav[#] to each anchor link.. it’s not the most convenient of things…

The HTML:

  <ul class="nav">
    <li><a href="index.php" class="<?php echo $nav1; ?>">home</a></li>
    <li><a href="publications.php" class="<?php echo $nav2; ?>">publications</a></li>
    <li><a href="research.php" class="<?php echo $nav3; ?>">research</a></li>
    <li><a href="cv.php" class="<?php echo $nav4; ?>">cv</a></li>
    <li><a href="contact.php" class="<?php echo $nav5; ?>">contact</a></li>
  </ul>

The PHP:

<?php

function getUrl() {
  $url  = @( $_SERVER["HTTPS"] != 'on' ) ? 'http://'.$_SERVER["SERVER_NAME"] :  'https://'.$_SERVER["SERVER_NAME"];
  $url .= ( $_SERVER["SERVER_PORT"] !== 80 ) ? ":".$_SERVER["SERVER_PORT"] : "";
  $url .= $_SERVER["REQUEST_URI"];
  return $url;
}

$myurl = getUrl() ; // As was suggested, I could also just set $myurl = $_SERVER['PHP_SELF'] and avoid the complicated function call

$split_url = explode("/" , $myurl) ;

$page = end($split_url) ; 

$nav1 = $nav2 = $nav3 = $nav4 = $nav5 = ' ' ; 

if ( $page == "publications.php" )
    $nav2 = 'current' ; 

else if ( $page == "research.php")
    $nav3 = 'current' ; 

else if ( $page == "cv.php" )
    $nav4 = 'current' ; 

else if ( $page == 'contact.php') 
    $nav5 = 'current' ; 

?>
  • 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-07T19:55:50+00:00Added an answer on June 7, 2026 at 7:55 pm

    Take a look at $_SERVER['PHP_SELF']. It will give the file name of the current file being served.

    Here’s a function that will add the current class if the page matches the $_SERVER['PHP_SELF'].

    <?php
    
    function add_current_class($page_name) {
      if( $page_name == $_SERVER['PHP_SELF'] ) {
        return ' current';
      }
    }
    

    Usage would be as follows.

    <ul class="nav">
       <li><a href="index.php" class="<?php echo add_current_class('index.php'); ?>">home</a></li>
       <li><a href="publications.php" class="<?php echo add_current_class('publications.php'); ?>">publications</a></li>
       <li><a href="research.php" class="<?php echo add_current_class('research.php'); ?>">research</a></li>
       <li><a href="cv.php" class="<?php echo add_current_class('cv.php'); ?>">cv</a></li>
       <li><a href="contact.php" class="<?php echo add_current_class('contact.php'); ?>">contact</a></li>
     </ul>
    

    If you wanted to really make it special you could do something like this.

    <?php 
    
    function display_navigation() {
      $pages = array(
        'index.php' => 'home',
        'publications.php' => 'publications',
        'research.php' => 'research',
        'cv.php' => 'cv',
        'contact.php' => 'contact'
      );
    
      $link = '<li><a class="%s" href="%s">%s</a></li>';
    
      echo '<ul class="nav">';
      foreach( $pages as $page => $text ) {
        printf($link, add_current_class($page), $page, $text);
      }
      echo '</ul>';
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am developing an events page using jquery mobile. I have a stylish way
I have a jQuery selector that is running way too slow on my unfortunately
I have a simple jquery flipcard animation - http://jsfiddle.net/gGAW5/36/ Now the way this flip
If I have 3 radio buttons, is there a way through jQuery of finding
Is there a way to delay css from firing using jquery? I have some
Is there a better way to do this jquery code? Currently I have to
I have jQuery on my site. I also have a youtube video using a
I have a list like the following: <ul id=links> <li><a href=/example-url-1>Link 1</a></li> <li><a href=/example-url-2>Link
Is there a way to use jquery to select all checkboxes on a page
In the latest build of jQuery Mobile, the way to add a back button

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.