Two Blogs on One Page

(Using PHP)

Why? What? Huh?

My blog, Mister Crunchy, is really two blogs. On the left side, I write longer pieces. On the right, I post links to stuff I think is worth linking. I used to publish all of it in a single blog, but I didn't like that when I posted lots of links they would push my longer pieces down and out of sight. I like my original work to stay visible. I'm vain that way.

I thought it would be easy to put two blogs on one page. I expected that maybe Blogger would have a setting somewhere that would let me do it. Alas, Blogger didn't support it and none of the Web programming books I read included a chapter on putting two blogs on a page. So I read up on server-side scripting and then farted around until I got something working.

I don't claim that the method I'm going to show you is the best way to put two blogs on the same page or the only way. What I can say is that it works.

PHP

This method uses a scripting language called PHP, which stands for "PHP Hypertext Preprocessor." That's right, the acronym refers to itself. If your blog's host doesn't support PHP, you either need to upgrade your hosting account to support it, or you need to find a new host. This method won't work with Blogspot or probably most other blogging tools which host your blogs for you. You need your own host.

PHP statements are pieces of code, little programs, really, that your web server executes before sending a web page out to a visitor. One of the things we can do with PHP is write a little program to grab an external text file and slap it into the middle of your web page before sending it out to your visitors. Once we've got PHP code grabbing one file, what's to stop us from grabbing two? Nothing. Like every other Web page, a blog is just a fancy text file. So if we can include two text files on our page, we can include two blogs.

How It's Done

Ready to learn the secret? Here it is:

<?php include 'filename.txt' ?>

That's it. That one line is a PHP command which tells the server to replace it with a file called "filename.txt". Actually, it's not quite that simple. You also have to change your page's filename so that the extension is ".php" rather than ".html" or ".htm". Changing that extension tells the PHP-supporting web server to look for PHP code to execute.

Now that you know how to include external files on your web page, it's pretty easy to put two blogs on there. You build a page with all your formatting and layout. You include PHP statements instructing the server to insert two text files into your page, and you set up two blogs in your blogging software with those filenames. It's probably a good idea to remove everything from your blog templates up to and including the <body> tag at the beginning, and everything from the </body> tag onward from the end. Having multiple <head> or <body> tags in an HTML file gets confusing for some browsers.

Columns

Another issue you might face once you manage to get two blogs on one page is how you'll get them to display side-by-side. Probably the easiest way to achieve this is to make a table, but that's so 1998. I use CSS positioning, instead. It's a little harder to set up, but it's worth it. It makes your code much easier to read and maintain. This page you're looking at right now uses CSS to display two columns of text. Feel free to view the source and borrow whatever looks useful to you. All the necessary styles are in the <head>. The #leftpanel and #rightpanel divs hold the columns. The #container div centers the whole thing, using the nifty auto margin trick.

For more on CSS multi-column layouts, check out BlueRobot or glish.

Example Page

Speaking of viewing source, wouldn't it be nice if you could see a great example of how to include two text files on one page by looking at the source of this page? After all, this page uses PHP to include two text files. Try it. Right click (or do whatever you Mac people do) to View Page Source. Huh? The PHP code is nowhere to be found. That's because by the time the file gets to your browser, my web server has already executed the PHP code, replacing it with the contents of two text files.

This is easily remedied. Here is an exact copy of this page with one small but critical difference: the file extension is ".txt" instead of ".php". Without that "php" extension, the server doesn't execute the PHP code. It displays it. If you want to see what the two included text files look like before the server includes them on my page, click here and here.

If you want to get a two-column PHP page up and running as fast as possible, you could just save the .txt verson of this page, upload it to your PHP-enabled server, and rename it with the .php extension. Then you can put whatever you want, including blogs, in two text files with the appropriate names, and you're off to the races.

If you'd rather set up your two-blog layout with Server Side Includes instead of PHP ('cuz you roll oldstyle), here's an SSI example page.

How Can I Ever Repay You?

At the bottom of this page, I've included Amazon links to four books and a piece of software that I use all the time to make my websites better. If you follow one of those links and buy anything, I'll get a little kickback. Nothing says "Thank you" quite like a kickback.