在页面或子页面上使用PHP将活动类添加到导航链接?

在页面或子页面上使用PHP将活动类添加到导航链接?

问题描述:

I see a lot of these kinds of questions around this website, and on the web, but I can't find the specific answer to what I'm trying to do. I don't want to use JS, I want to use it as little as possible, but if I have to I'll resort to it.

I want to use PHP to add an active class to my nav links when that page is open, or if a page in the drop down menu (the sub-page's I was referring to) are open. Here is my code.

In all of my pages I have "included" the following:

<?php include('includes/header.php'); ?>

As part of that included page I have the following:

<ul class="firstbar">
    <li class="item"><a href="index.php">Home</a></li>
    <li class="item has-dropdown">
        <a href="about.php">About Us</a>
        <ul class="dropdown">
        <li><a href="about.php#about">G2 Drivers</a></li>
        <li><a href="about.php#locations">Locations</a></li>
        <li><a href="about.php#instructors">Instructors</a></li>
        </ul>
    </li>
    <li class="item has-dropdown">
        <a href="services.php">Services</a>
        <ul class="dropdown">
        <li><label>In-car</label></li>
        <li><a href="services/defensivedriving.php">Defensive Driving</a></li>
        <li><a href="services/incarlessons.php">In-car Lessons</a></li>
        <li class="divider"></li>
        <li><label>In-class</label></li>
        <li><a href="services/curriculum.php">Curriculum</a></li>
        </ul>
    </li>
</ul>

You can take a look at the test url here: www.mrobertsdesign.ca/g2drivers/ (the active class is already available, just not sure how to go about adding it)

The active class needs to be added to the list item tag right before the anchor link tag, like the following:

    <li class="active item"><a href="index.php">Home</a></li>

The about.php page has a dropdown menu but it's only hash-linked to parts of that page. But for pages like services.php which has a drop down to different pages, I want the 'Services' list item tag to have the active class as well, so nothing different. Like the following:

    <li class="active item has-dropdown">
        <a href="services.php">Services</a>
        <ul class="dropdown">
        <li><label>In-car</label></li>
        <li><a href="services/defensivedriving.php">Defensive Driving</a></li>
        <li><a href="services/incarlessons.php">In-car Lessons</a></li>
        <li class="divider"></li>
        <li><label>In-class</label></li>
        <li><a href="services/curriculum.php">Curriculum</a></li>
        </ul>
    </li>

The thing that I can't find in any of the questions that I've looked at so far is if one were to be currently on one of the pages in the drop down menu, most of the questions don't seem to include the idea of a drop down menu and the code usually includes finding the current page, etc, so there's no way so far for me to use that for this problem.

All help is appreciated! Thanks!


@1andsock answered the first half of the question below, but I have a couple pages that are in different folders and have the same name so the script doesn't work in those situations. For anyone who wished to take a look and would like to help me out with that, that would be awesome =)

Add class="active" to active page using PHP

That is a similar article to this one, that I just saw, and it seems to answer what I'm looking for, but it has a 'noactive' class, I just want it to be 'active' or just not display active.

I haven't tested this, but why can't you just do:

<?php
  $current_page = basename($_SERVER['PHP_SELF']);
?>

and then:

<li class="<?php if ($current_page == "index.php"){ echo "active "; }?> item"><a href="index.php">Home</a></li>

for normal links, and:

<li class="<?php if ($current_page == "index.php"){ echo "active "; }?> item has-dropdown">
    <a href="about.php">About Us</a>

for links with dropdowns?

If you are using sessions, you could set a current page session variable and use it on the page:

On each page:

session_start();
$_SESSION['current_page'] = 'this_page';

Then on your menu page:

<li class="<?php if ($_SESSION['current_page'] == 'this_page'){ echo 'active '; }?> item"><a href="index.php">Home</a></li>