I get a lot of people sending me e-mails, or using my Q&A thing to ask me how I did my dynamic/changing sidebars on the site. I think people over-complicate it and assume it’s a magic PHP thing when really, it’s not. Now, you have to bear in mind I don’t use WordPress and my pages are all static to cut down on unnecessary database queries, but generally there’s no reason why anyone should have problems applying the same code to their own sites.
Firstly, my site relies on a handful of files that I request using
include() a.k.a. “PHP includes”. I have a header, which stays the same, and then a footer for each section of the site. That is effectively the core of the changing sidebars. My site is then structured into actual folders (I don’t use URL rewriting or anything snazzy there) and the files in each folder reference the main header (using “../” to go up a folder), and the individual footer. It’s that simple.
Here’s a visual example of my folder structure:
root folder index.php header.php footer.php about index.php footer.php
Each footer.php contains the sidebar structure code (e.g.
<div id="sidebar">), and then the custom text for each section. So my main footer has blog archive links, the About footer has the About sub-section links, and so on. The index.php page for each section (and each extra file) end up looking a bit like this:
<?php include('../header.php'); ?> some text here <?php include('footer.php'); ?>
…and that’s it. Saves having to create conditional statements and/or checking URLs. Of course, the only downside is having to update multiple footers if I change my coding about, but it’s hardly a huge chore.