How to Implement a Sitewide Sidebar

In most cases, should you choose to have a sidebar on your site, this will contain links or navigation which is intended to be sitewide ie. on every page of your website. Especially in the case of a navbar, if something about your site should change, you will want to update this on your side links. However, it can be time-consuming and can require concentration to go through every single page and update it. Consequently, there’s something you can do which removes this problem and makes your code overall cleaner.

The way to do this is with a PHP include function. Now before you quail at the mention of PHP, don’t worry; you don’t actually need to know any of the language in order to pull this off. The first and perhaps most important thing to do when considering PHP is that you should rename any file containing PHP code to have the extension .php, as opposed to .html.

With the sidebar, make sure you have it designed exactly as you like, but also so it is not dependent on the rest of the HTML code. Once you’ve done that, cut and paste it into a separate document and name this ‘sidebar.html’ or ‘sidebar.php’. Now comes the trick. In all your other site pages, now named something.php, where the sidebar should be, include this code:

<?php include(“sidebar.html”); ?>

and change the filename as appropriate. This then includes whatever code you have in the seperate document, so when you make a change to the sidebar file, it shows up on all the pages that have the include function. It also reduces all the sidebar code that was previously in every document into a manageable, one-line chunk.

There you have it – simple! You can do the same with headers, footers and any other parts of the site you want to be universal and readily changeable. Get coding!


One Response

  1. A blog like yours should be earning much money from adsense.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

© Aaron's Driving School
CyberChimps