简体   繁体   中英

Dynamic Navigation Sidebar Content

I have a navigation sidebar on my site with all of the main site pages, where every page has the same links, except the current page isn't a link, so you can visually tell which page you're on, and there's no reason for a link to the current page. Currently, I'm doing this manually, copying and pasting the list of links every time I create a new page, then replacing the link to the current page with plain text. However, then I have to go back to all of the existing pages and add the new page. Is there any way to create a list of links in an external file, maybe a JS script or something, that dynamically removes the link on the list item for the current page, replacing it with just text, so I can just import it in my page template and update all of the pages at the same time when I add a new page?

It's a pretty simple list of links. For example, here's the list as it appears on the /overworld/lightworld page.

<div id="sidebar">
  <ul id="nav">
    <li>
      Overworld

      <ul>
        <li>Light World</li>

        <li><a href="/overworld/icyworld/">Icy World</a></li>
      </ul>
    </li>
  </ul>
</div>

And here's the same list on the /overworld/icyworld page:

<div id="sidebar">
  <ul id="nav">
    <li>
      Overworld

      <ul>
        <li><a href="/overworld/lightworld/">Light World</a></li>

        <li>Icy World</li>
      </ul>
    </li>
  </ul>
</div>

The list is longer than that, but that's the idea.

You should use simple PHP .

Create a file called nav.php , for example. Put in it your menu. Then, to call this file from any other location, simply put this:

<?php include('nav.php'); ?>

Then, if you want to change given link on every site, you just need to modify it once in nav.php file.

PS - all of your files must have .php extension, otherwise it won't work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM