Dynamic/Changing Sidebars

 |  Interwebs

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


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.

Jem Turner jem@jemjabella.co.uk +44(0)7521056376

15 comments so far

  1. Rachael said:

    Do I have to make a comment along the lines of “I can’t believe you’re talking about this like it’s new sh*t” ? :P Seriously, though, it’s a useful technique for people who don’t already know it. Yay for enlightenment! Is that a word?

  2. Hev said:

    Hey Rachael, I didn’t know how she did it, lol. I figured it was php includes, but wasn’t sure. I just figured it was part of Jem’s blog coding. I should have figured it would be something really simple. Thanks Jem.

  3. Ruth said:

    I used to do something similar (when I had enough stuff on my site to bother, that is). Just curious – what’s in your root folder footer.php?

  4. Niki said:

    I figured it out before you posted this. I thought you used some kind of pwning PHP ninja script more advanced than includes. Lol. Happpy belated birthday, Jem! It was my dad’s and friend’s birthday yesterday, too.

  5. Carly said:

    Yes, happy belated birthday… I just got my head around conditional statement type things in wordpress, all along there was a much simpler way of doing it! Woo…

  6. Vera said:

    Hm… I remember once I read a review… on captious pedants I think, where they asked the person why the need to change the folders and not just have the images pertaining to it in a different folder. Aside that, I always test my coding offline, and like for it to be flexible so that it works with minimal changes online as well. If I have subfolders, I always have to use the absolute path… which I forget to change either offline or online… So mine is somewhat based on your tutorial for custom titles with PHP. I have a $section variable which has 4 different values, depending on where I am (me, you, site, home). I define this variable before I call the include functions. Next, I have 5 different footer files (I have the sidebar info in my footer) one for each section and a generic one. In the generic footer file, I have a switch statement, which checks the value of $category, and then includes the correct file. eg: switch($category) case “me”: include(footer1); case “you”: include(footer2); case “site”: include(footer3); default: include(footer_def); endswitch ^ don’t quote me on the syntax. I looked it up, but promptly forgot it after using True, the downside of this, is that I have to specify the $category in every single file. The upside is, that I don’t have THAT much trouble with fixing links from the offline – online version, and the other way around…. … um… did I make sense? =P

  7. Kristina said:

    You’ve just totally frazzled my already highly confused brain, Jem. I like the changing sidebar, thought. ^^; Hah, what a stupid comment compared to everybody else’s intellectual commentism.