Adding the “Blog” page with PHP

Posted by Greg August - 10 - 2009 - Monday

As mentioned in a previous post, I wanted to add a link to the “traditional blog” layout… just a bunch of posts in reverse chronological order. When my dad came over for a visit today, he reinforced this need– “I like your new layout, but how do I just go to the blog?”

Seemed simple. The Androida theme, upon which the new layout is built, includes 2 different layout options– “business” or “blog”. Surely I just have to link to an appropriate .php file to get what I want, right? Nope… doesn’t work that way. So I set off to find the solution. Now, I don’t know why I couldn’t find any solutions through Google, but I couldn’t. I don’t know why I couldn’t get a plug-in to do this for WordPress, but I just couldn’t find one.

So, I cooked up my own “hack” for the theme. It’s not pretty… there’s definitely a way to make it more integrated with the theme options. My way is hard-coded, which means that the theme isn’t portable without manual tweaking. So, I’ll clean it up some day if I ever want to contact Web2Feel to ask them if I can release an Androida Redux theme. But in the meantime, I had to have that link, so the quick and easy solution was the way to go.

How it’s done

What Needed Changing

WordPress loads an index.php page from its root directory, which in turn loads another index.php page from inside your theme folder. When you look at Androida’s particular index.php it looks like this (formatting my own):

1
2
3
4
5
6
7
8
9
10
<?php get_header(); ?>
<?php  
  if ($layout = get_option('andro_layout'))
  {
    include (TEMPLATEPATH . '/layouts/'. $layout .'.php');
  }
  else
  include (TEMPLATEPATH . '/layouts/business.php');  
?>
<?php get_footer(); ?>

In English: Get the header (loads header.php). Then check to see if there’s ANY option set in Androida (just a sanity check). If there is, use the template found in the template directory, plus the subdirectory “layouts”, with the filename $layout.php. If you have set an option in Androida, the $layout variable translates a “business” or “blog”, so the filename becomes either “business.php” or “blog.php”. Otherwise, if there’s no option set at all by Androida, use “business.php” (in other words, business.php becomes the default). Then get the footer (footer.php).

This means that you can only use one layout or the other with default Androida.

How I Changed It

However, PHP has a $_GET function which I can take advantage of. All this function does is “look at your address bar” and extract any variables that appear after a “?”. You’ve all seen these… in your address bar you’ll see something like “http://www.mywebpage.com?username=Greg&favhockeyteam=Senators”.

So, all I needed to do was invent an arbitrary new variable (I called it “pagetype” but you could call it “fuzzykittenpaws” if you want) and use it to tell Androida’s index.php something new.

In English: I wanted it so that whenever you put “http://blog.monkey-house.ca?pagetype=blog” in your address bar, you would see a blog layout.

For this to work, index.php needed to understand what to do with “pagetype”:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?php
$pagetype = $_GET['pagetype'];
?>

<?php get_header(); ?>

<?php
if ($pagetype == 'blog') {
    include (TEMPLATEPATH . '/layouts/blog.php'); }
else {
    if
      ($layout = get_option('andro_layout'))
    {
    include (TEMPLATEPATH . '/layouts/'. $layout .'.php');
    }
    else
    include (TEMPLATEPATH . '/layouts/business.php');
}
?>
<?php get_footer(); ?>

Most of the code is the same… that whole middle part is identical, you’ll notice. I could have “refactored” the code, and in fact I could have used the $layout variable instead of my new $pagetype… but that would require more refactoring than it was worth. So,

In English: Declare a new variable called $pagetype by using $_GET to grab it from the address bar (if there is no ?pagetype=blog in the address bar, $pagetype will become “null”, which is fine). Get the header (just like before). But now, first thing to do is check to see if $pagetype is “blog”. If it is, load the blog.php template file. This implies that if it is NOT “blog”, execute the rest of the original Androida code. Then get the footer.

Adding a Link to Navigation

The final thing I needed was a link. This is where the hard-coding breaks the portability of the theme. I opened “header.php” and located the following:

1
2
3
4
<ul class="lavaLampBottomStyle" id="A">
    <li><a href="<?php echo get_option('home'); ?>/">Home</a></li>
    <?php wp_list_pages('sort_column=menu_order&depth=1&title_li=');?>
</ul>

This is located inside my menu div. All it does is set a “Home” link, which gets a valid URL from WordPress, and then uses the standard “Pages” list to generate any links based on “Pages” I have (as of this writing, I have none… but that might change).

All I had to do is add an extra item to the list… a hard-coded link containing the URL with the pagetype variable–http://blog.monkey-house.ca?pagetype=blog.

1
2
3
4
5
<ul class="lavaLampBottomStyle" id="A">
    <li><a href="<?php echo get_option('home'); ?>/">Home</a></li>
    <?php wp_list_pages('sort_column=menu_order&depth=1&title_li=');?>
    <li><a href="http://blog.monkey-house.ca?pagetype=blog">Blog</a></li>
</ul>

It will actually be relatively easy to change this hard-coding to make it portable… it just needs to become another Androida option. But that’s for another day. ;-)

Leave a Reply

About us

Monkey House is populated by three lovely and wonderful simians–Greg, his wife Alex, and their son Cole. He is a jack of all trades, she is a scientist/athlete, and their son is a poopsmith.