The WordPress ‘Default’ Theme Demystified – Part Two
In Part 1 of my WordPress tutorial I discussed the file that is at the ‘heart’ of your site, index.php. In Part 2 of this tutorial, I will focus in depth on some of the PHP includes that are called inside of index.php and show you how to edit, modify and add more functionality to your pages. So without further ado, let’s get started.
The core of the index.php file is The Loop. WordPress uses The Loop to find out if there are any posts to display, how many to display and to stop when there is nothing left to display. The Loop starts with the line <?php if (have_posts()) : ?> <?php while (have_posts()) : the_post(); ?> and ends with the line <?php endif; ?>. There is some code that come before and after this section that make calls to the header, the sidebar and the footer templates within the ‘Default’ Theme directory. The ‘template tags’ used to call the PHP files are <?php get_header(); ?>, <?php get_sidebar(); ?> and <?php get_footer(); ?>, respectively. WordPress uses these pre-determined ‘template tags’ to bring each file’s content into the main template. However, you can also use normal PHP syntax to call in the header, sidebar, footer or any other file you like. Let’s look at an example.
Adding a different sidebar
It’s easy to make changes to the ‘Default’ Theme’s sidebar.php file via your favorite text editor and add the things that you want. You may choose to include a small ‘About’ paragraph, a recent comments plugin or a link to your site’s RSS feed. That’s all fine and dandy, however, once a user clicks on an individual post to read more maybe you don’t want them to see that same sidebar information they saw on the Home Page. Perhaps the information that was displayed there is irrelevant now that you’ve gone one level deeper into the site. Or maybe you’d like to include links to more recent posts, a search bar or even some meta data and stats about that particular post. Whatever the case may be, there is an easy way for you to call in another ’sidebar’ that may be more appropriate for the individual post page.
First thing’s first, we have to understand what happens once the Permalink title is clicked. Once you click the title to read an individual post, WordPress loads up single.php. This PHP file is simply a modified version of the index.php and is displayed when you are viewing a single entry. Novel idea, huh? So let’s say that you wanted different information to appear in the sidebar when viewing single.php. You only have one sidebar template, so you’ll have to use a little bit of ingenuity to figure out a way to get something else in its place. The way that I like to go about this is to create a copy of sidebar.php and start working from there. You can rename your duplicate anything you like, we’ll go with side.php to keep things simple. Once you have your file renamed, you can treat it just like you would sidebar.php. Add whatever you like to it, the difference will be in how we call it via single.php.
So now that you have side.php set up the way you like it, we will have to make one slight change to the single.php file. Look for the line of code that contains <?php get_sidebar(); ?>. Remove that line and replace it with <?php include @ ("side.php"); ?>. What this does is tells WordPress to use a PHP include for side.php instead of using a ‘template tag’ to call sidebar.php. It’s as easy as that. Your file side.php should now occupy the same spot that sidebar.php did in the Home Page. Now let’s go a little bit deeper with some custom templating.
Custom Templating
As you can see there are more ways than one to look at the code contained within the WordPress templates. Once you become comfortable tinkering around under the hood, you can really start peeling back the layers to get to the good stuff. Custom templating is said “good stuff”. Wouldn’t it be nice to create your own WordPress page? One in which you create the flow of information, either static or dynamic? Well you can, and quite easily in fact. One of the easiest ways involves adding a snippet of code to the top of your .html file and then referencing that file via your WordPress Administrator Panel.
The profile section of this site is a Page that I created in the Administrator Panel and is an example of how to go about customizing your own template files. I wanted the page to contain static information about me, but I also wanted to incorporate some of WordPress’ dynamic capabilities. So I coded up the page and laid everything out the way I wanted it. Then I added this portion of code to the top of the XHTML document:
<?php /* Template Name: Profile Template */?>
This code tells WordPress that this file is called ‘Profile Template’ and is available to use as a custom template. After saving the file in my ‘Default’ Theme directory as profile.php, I am now able to reference this via the Admin Panel. Log in to WordPress as admin and go to ‘Manage’. Choose ‘Pages’ and from there, type in the name of your page in the ‘Page Title’ box (in this case it would be ‘Profile’). Then go to the ‘Page Template’ drop down box and look for your custom template called ‘Profile Template’. Select this option, create your new Page, and then select ‘View Site’. Click on the Page link that you just made and you should see your custom template appear. Pretty cool huh? This is probably the easiest way I’ve found to create custom templates. For something a little more structured and advanced, I use the PHP include <?php include @ ("../includes/side.php"); ?> to call all of my files into index.php and keep my custom files in an ‘includes’ folder. Not only does this keep everything more organized, it also makes it easier to change just one file as opposed to every file that makes reference to that portion of code. This requires a little bit more working knowledge of The Loop and PHP which can get complicated. But for starters, you can follow the example above to incorporate your own templates into the WordPress flow and soon you’ll be adding more custom functionality to your site.
What to expect in Part 3
Next time I hope to talk a little bit more about customization and get more advanced with The Loop. I will show some directory structure and layout that may be beneficial to folks who are looking to use WordPress as a way to add dynamic functionality to their site, but not take precedence over layout and design. As always, please feel free to ask questions if something doesn’t make sense or suggest topics for the next tutorial.
In Part 1 of my WordPress tutorial I discussed the file that is at the ‘heart’ of your site, index.php. In Part 2 of this tutorial, I will focus in depth on some of the PHP includes that are called inside of index.php and show you how to edit, modify and add more functionality to your pages.
Just wanted to post to serve as a motivation for you to continue the series! These are great man, really helping my knowledge of WP. Excellent work!
Hia!
I’ve been reading through part 1 and 2 of your Wordpress articles, but I *shame* have a few questions if that’s ok with you :)
To be honest, I can make websites with good css/xhtml 90% of the time (i’m still a “noob” though, hehe). But the main thing I don’t seem to understand is how to actually change the looks of (any) Wordpress theme.
I’ve been looking into the PHP-files, but so far the only thing I managed to change, was some of the CSS. Changing the basic things like fonts, background and such things aren’t really the problem. But when it comes to actually replacing/removing stuff of the website-looks itself, things start to get blurry, hehe.
Would it be possible to include a tutorial of some sort into one of the articles, which contains a step-by-step explanation of how to modify the looks? Maybe even some basic stuff on how you changed the looks of your own site? :-)
On a sidenote;
I think I speak for all readers of your Wordpress articles when I say you rock dude! Maybe this time I’ll start to understand some minor PHP magic ;-)
Thanks a lot for your feedback guys. It’s nice to know that some of the stuff I’ve been writing about is doing some good.
I don’t know if you have read some of the other entries but I am moving across country in a mere week so my time is being taken by packing and selling my belongings. I will however get a tutorial together sometime in the next couple weeks that will give a demonstration on how to go about ’skinning’ the Default Theme.
Again, thanks for the positive remarks and I’m glad you enjoy the write-ups.
Ah, take your time, I’m not going anywhere anytime soon (just moved myself, hehe).
I’m looking forwared to the “noob”-safe tutoria! :D
Thanks for taking the trouble of writing those Wordpress articles, and good luck moving!
Regards,
Joram
Bummer… forgot to mention something :)
I just tinkered with the red_is_nice template. But as far as I could tell, that template looks way easier then the Default-template that comes with Wordpress.
I’ve already fiddled with the CSS and (the easiest part) the images… But this template has less files then Default if you look in the “presentation->edite template” preferences.
Well, i thought I’d let you know… perhaps this template is easier to work from for us ;)
Thanks for this and your other Wordpress tutorial. I’m finding it incredibly helpful in getting my wordpress CMS up and running as a complete newcomer. It’s well written, makes sense, and approachable.
Thanks for the great info, Matt.
I’m curious if anyone knows what CMS platforms content rich platforms like Businessweek.com are using. Any comments or suggestions would be greatly appreciated.