user2162270
user2162270

Reputation:

Dynamic Navigation?

Currently for my projects I create the navigation for each page manually and it looks something similar to:

<nav>
  <ul id="mainMenu"><!--Main Menu-->
     <li><a class="active" href="index.php">Home</a></li>
     <li><a href="contact.php">Contact</a></li>
  </ul>
</nav>

This works fine, however for projects that has many many pages it is not a really good practice or even efficient to do it manually. So I was wondering if there is anyone who can direct me to the right path and advice me on how to make my navigation dynamic? I know about PHP include and the .inc files - they are good. BUT I want to add class .active to the <a> of the page that is currently open. How can I do that?

BTW: I don't know if this s the right place to post this sort of questions here, but the moderator told me to post it here.

Upvotes: 0

Views: 153

Answers (1)

nfechner
nfechner

Reputation: 17525

Use include to add a central php file, that contains a function which can take the current page as a parameter:

nav.inc:

function renderNavigation($current_page) {
    //render you navigation
}

main.php:

require_once("nav.inc");
renderNavigation("Subpage 1")

Upvotes: 1

Related Questions