Some WordPress News from WordCamp San Francisco
For all you fans of WordPress, this weekend has been a good one. Although I personally wasn’t able to attend the WordCamp San Francisco event, many were able to go and we’ve been receiving a lot of great information.
As a result, I wanted to do a quick post to bring to your attention a couple of the things we are hearing from the event, and in particular, from Matt Mullenweg’s “State of the Word” address, which he does at each WordCamp event:
- WordPress 2.8 Coming VERY Soon!
- There have been hints from the WordPress team about adding a section to their theme repository for GPL-compliant premium themes (paid themes which comply with GPL). I haven’t gotten clarification yet if their will also be a similar site setup for premium plugins that comply with GPL.
- WordPressMU and WordPress.org will be merging.
To those of you that attended the event, let us know about anything we missed in the comments!
Information Sources:
- The WordCamp Report from the event.
- Various tweets from a number of web desginers in attendance.
WordPress 2.8 Moves to Beta 2
Just a quick note to our readers know that WordPress 2.8 has now moved to Beta 2. You can see the changes here. If you are interested in giving it a try, you can download it here.
Note: If you aren’t comfortable using beta software, we recommend you hold off on using WordPress 2.8 until the first release candidate is announced.
How To: Hack WordPress Theme Template Pages
The key to being able to display exactly what you want in Wordpress is understanding Wordpress theme template pages. These are the theme files that display pages, not the ones that perform functions like comments, sidebar, etc. Most of us don’t use the Wordpress default theme that comes with installation, and end up downloading a free theme from the Internet. This is a great way to customize your blog, but not all theme authors code their theme the same way. The capabilities of that theme largely depend on how much time the web designer took to code it, in addition to their knowledge of Wordpress itself.
I’m going to explain everything you need to know to be able to customize all your theme pages any way you want, and this will give you enough information to begin coding your own theme as well. Even if you’re an ‘expert’ theme coder, you should learn something new from this article.
How Wordpress Works
The most important thing you could learn about Wordpress is the Template Hierarchy, or – “the order in which Wordpress calls pages”. The ONLY file that is required in the PHP files of any Wordpress theme is the “index.php”. That’s it! That one file could handle every single function Wordpress performs (if you wanted it to). Or, you could have a Wordpress theme that had a PHP theme for for every single WP function (or anything in between).
The Order of Things
Every time a Wordpress page is called the WP ‘engine’, if you will, determines (through process of elimination) what kind of page it is. It’s kind of like a “where am I?” function. Wordpress says “what page am I…” and in turn tries to call pages in a specific order. If WP doesn’t find the PHP file it needs it just defaults to the “index.php” file and uses it instead. There are 9 basic kinds of pages Wordpress looks for first:
Am I the Home Page?
If WP thinks it’s on the home page it will look for “home.php” first, and “index.php” second.
Am I Post Page?
(Single) post pages look for “single.php” first, and then default to “index.php”.
Am I a ‘Paged’ Page?
(Static) or ‘paged’ pages in Wordpress look for a “pagetemplate.php” first (if assigned when published), “page.php” second, and default to “index.php” last.
Am I a Category Page?
When Wordpress determines it’s on a category page first it looks like a category specific ID page, such as “category-7.php”. If it doesn’t find that it next looks for a “category.php” (which would be used on every category page). If that’s not there is searches for “archive.php”, and last it defaults to “index.php”.
Am I a Tag Page?
If Wordpress is on a tag page it tries to load “tag-slug.php” first, with ’slug’ being the name of your tag. If your tag is ‘wordpress hacks’ the tag slug page would be “tag-wordpress-hacks.php”. It that’s not available, WP next looks for “tag.php” which would load for all tag pages, then “archive.php”, and if that’s not there last it defaults to “index.php”.
Am I an Author Page?
If your blog has multiple authors, first it looks for “author.php” to display the details. If that’s not there, it tries to load “archive.php”, and last it defaults to “index.php”.
Am I an Archive Page?
Archive pages are loaded when Wordpress loads a date based page for previous posts. First it tries to load “date.php”, then “archive.php”, and last it defaults to “index.php”.
Am I a Search or 404 Page?
If WP determines it’s on a search (results) or 404 (not found) page the it tries to load either search.php or 404.php. If not, the default is once again “index.php”.
Am I an Attachment?
Out of all the Wordpress theme template pages, the attachment page is probably the one used least, and I have to admit – I’ve not seen a single one of these in any of the hundreds of themes I’ve downloaded. Wordpress uses these special pages usually for uploaded content, which would explain why it first looks for “image.php”, “audio.php”, “video.php”, or “application.php”. Then it tries to find “attachment.php” or “single.php”, and if none of those are available it also defaults to “index.php”.
Inner Workings of WP Theme Templates
As I said before, you could use a single index.php file to handle the 9 types of pages. You would simply code in some conditional tags, like I showed you in the last tutorial I wrote here on WP Hacks. A single index.php would then just contain code to say if is_home, do this, if is_single do that, etc. That’s a lot of code for just one page, and a bit unorganized – and it doesn’t leave a lot of room for customization.
Coincidentally, like Wordpress searches for 9 basic pages – each theme template page also contains 9 basic Wordpress elements:
- a header call
- opening of ‘the loop’
- a call to get the permalink and (some) meta
- a call telling Wordpress what to get
- a call to get either the content or an excerpt
- (maybe) more meta
- closing of ‘the loop’
- a sidebar call
- a footer call
Those are only the Wordpress elements, of course the PHP code to make them work is usually scattered throughout the appropriate HTML code make your theme’s layout and graphic design work properly. I’m going to explain these elements a bit more so you can understand how you can customize (or create) nearly any theme template page.
Header, Sidebar, and Footer calls
I’m going to handle all 3 of these elements at once, since they are all basically the same. When you see this code in a template:
<?php get_header(); ?>
Wordpress is simply opening the “header.php” file. The same is true for get_sidebar (sidebar.php) and get_footer (footer.php). You could have multiple headers, footers, or sidebars, see the earlier link above for conditional tags.
Opening of “the loop”
The infamous “Wordpress Loop” is when a call goes out to the database to do something until Wordpress says “stop”, i.e. ‘get me the most recent full text posts in their entirety’. The structure of ‘the loop’ changes depending on what kind of page your displaying, and each of the 9 basic types of pages Wordpress tries to load has a ‘loop’.
The opening of the loop generally looks like this:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
You may see it broken down with have_posts on one line to define conditional tags with the while and the_post on another, but it’s still the opening of the loop, and it’s pretty much the same in all pages. One way to use the multi-line loop opending is to place a parameter between “if have_posts” and the rest by using query_posts in between to show only a single post, posts from a time period, the last post only, posts from certain categories, or even change the ordering of posts being iterated in the loop.
A Call to Get the Permalink and (some) meta
The very last section of the loop opening (the_post) actually makes individual data available through each iteration of the loop. This data referred to usually as “post meta” because it’s descriptors and identifiers for the individual content being looped through. Typically things like the permalink (URL), title, date, etc. I say ’some’ meta, because most themes show some things before the individual post content, and then some after – such as categories and tags.
Here’s a short list of things you can call in post meta: the_permalink, the_ID, the_title, the_time, the_author, the_author_email, the_author_posts_link, the_category, single_cat_title, the_tags, single_tag_titls, edit_post_link, comments_popup_link, comments_rss_link
Example code you might see for post meta would be something like this:
<div class="post" id="post-<?php the_ID(); ?>">
<h2><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h2>
</div>
A Call Telling WP What to Get
Next Wordpress will decide how much of the actual individual post content to get for you. How much is gathered from the database depends on whether your look uses “the_content” (to get it all) or “the_excerpt” (to get part of it).
(Maybe) more meta
As I previously mentioned, the common things to see after a post are assigned categories or tags, and sometimes you see an “edit” link here as well. Some themes even put date published meta after the post content.
Closing of ‘the loop’
The code looks like this:
<?php else : ?>
<?php endif; ?>
Typically it’s on more than one line in case you want to build an option in, such as a message “Sorry, we didn’t find anything”. After the sidebar, before the sidebar and footer calls, is where you typically find the “next” and “previous” navigation links.
Bastardized Loops?
Well, just because most loops look like the examples I just gave you, doesn’t mean you can’t bastardize them in just about any way you can imagine. I recommend you read the WP Codex page The Loop in Action for examples of archive, category, and single post formats – as well as static home page.
The Codex official page for the loop has several examples of how to place multiple loops in one page.
Perishable Press has a great tutorial for multiple loops, multiple columns – if you want to try and split your content up. They also have some great loop templates, in addition to a great tutorial of horizontally sequenced posts in two columns.
Conclusion
Armed with just a tiny bit of knowledge, you can hack just about any Wordpress theme template page to do just about whatever you want! Now that you understand (in great detail) how Wordpress calls it’s pages and how the loop works, you can conquer any task! Have fun customizing your blog’s theme!
This post was submitted by John Pratt, who writes many Blogging Tips, and how to create a Wordpress affiliate store.
WordPress 2.8 Beta 1 Available for Download
For those of you who love to be on the cutting edge, you’ll be happy to learn that the Beta 1 version of WordPress 2.8 is now available for download. As always, with these types of releases, we recommend you use it in a test environment, rather than using it on a live website/blog. I usually recommend people wait until the release candidates (RC) before you can be comfortable using it on a live site.
For those interested, here are a few of the things included with the WordPress 2.8 Beta 1 release:
- New Theme Installer routines
- Add CodePress syntax highlighting to Theme and Plugin editors
- Use “Custom Header” for menu text and revise Default theme to reflect change
- Separate Comments into a separate postbox, from Discussion postbox, on the Edit Post screen
- Make tags accessible without Javascript on the edit screen
- Don’t ask for confirmation when marking a comment as spam
- Don’t notify post author of own comments
- Allow the dashboard widgets to be arranged in up to four columns as set via the Screen Options tab
- Add column “Rating” in Administration > Links > Edit
- Improve installer to help people entering wrong email addresses
- Per Page option for plugins
- Show absolute date instead of relative date for scheduled posts
- Autosave post/page when pressing Control/Command+S
- Add toggle all button to the Gallery tab in the uploader
- Support more than one gallery on the same page
- Add per page option to Screen Options for comments, posts, pages, media, categories, and tags
- Show Tools menu for all users so they can access Turbo
- Fix most popular link category list
- Add description field for tags
- “Choose a city in the same timezone as you” for Timezone in Administration > Settings > General
- In upgrade process, provide better explanation for database upgrade message
- Enforce unique email addresses in Add/Edit users
- Hide things that need to be available to screen readers via offscreen positioning
- Use invisible class for hiding labels and legends
- Use a semantic class name for text targeted to screen readers
To those who are interested in putting WordPress 2.8 to the test, you can download it here.
How To: Use WordPress Conditional Tags to Hack Your Theme
By using simple conditional tags – it’s pretty easy to add some very basic hacks to your Wordpress theme to have more control over what’s displayed when.
Here are some things you could do with a conditional tags:
- Display something only on certain pages
- Display something only on certain categories
- Display something in header and footer only at certain times
- Display something only on sub-children of particular pages
- Display something only in the WP dashboard
- Display something in the sidebar only when certain conditions are met
- Do something only when there’s a “sticky” post
- Do something only when a “page template” is used
- Do something only for “author pages”
- Display something only on search or 404 pages
Let’s say you want something to display only on the homepage, or just category pages, or maybe just your 404 (not found) page – it’s quite easy to do. You don’t have to be a hardcore programmer (I’m not for sure) to implement these very simple theme hacks.
Where to Use Conditional Tags
It all depends on your your theme is structured. I’ve seen Wordpress themes that a single “index.php” file handle just about everything, and other ones that use only the home.php, index.php, single.php, archive.php, and category.php files. You could handle everything with a bunch of code in one file if you want using conditional tags, have individual files for each thing, or any combination in between.
Most themes I’ve encountered usually have an index.php and a single.php only. If you want to know what pages Wordpress looks for first in a theme before defaulting to the “index.php”, read the official Wordpress Template Hierarchy page.
Conditional tags are great because you can use them both in and out of the loop. You can use them directly in theme pages, but you could also use them in your header, footer, comments, and sidebar files. Actually the sky is the limit, and you have only your own imagination to limit you!
Conditional Tag Examples
The worst thing I see on most posts about conditional tags is that they don’t have a lot of examples for you to draw from – so I’ll try to give you a few ideas to get you started…
If This is XYZ page
One of the most common ways to use a conditional tag is to add a filter of sorts to tell Wordpress “if I’m on ‘XYZ’ WP page – then do this. The most common reason would be maybe to show certain things (ads, text, messages, graphics, scripts, flash) in certain places.
For example, let’s say you want to display a message to visitors on your home page only…
<?php if (is_home()) { ?>
Welcome, you will only see this message on my homepage!
<?php } ?>
replace “is_home” with “is_front_page”, “is_single”, “is_sticky”, “is_page”, “is_page_template”, “is_category”, “is_tag”, “is_author”, “is_date”, “is_archive”, or “is_attachment” to make your message show up on nearly any Wordpress page. Remember, once the condition is met, you can “do” anything – from including a file to showing special graphics, running a script, anything! You could use this condition for example to show an ad on just your homepage, or just single pages.
If this is XYZ page show this, else show that
The nice thing about conditional tags is the fact that you can have as many conditions as you want…like this:
<?php if (is_home()) { ?>
<p>Show this!</p>
<?php } elseif (is_single()) { ?>
<p>Show this instead!</p>
<?php } elseif (is_category()) { ?>
<p>Show something different!</p>
<?php else { ?>
<p>Show this if no conditions are met</p>
<?php } ?>
OR – you could even structure it in such a way that you lump some conditions together like this:
<?php if (is home() || is_single() || is_category() || is_page()
|| is_archive() { ?>
<p>Show this on all those pages!</p>
<?php } ?>
The double-pipe or || in the code signifies “OR”, so Wordpress knows, if this is home, or a single page, or a category page, or a “page” page, or an archive page – then so something.
Show Everywhere, Except…
Sometimes you want to show something everywhere possible except just one or two places…
<?php if (is_home()) {
}
else { ?>
<p>Show this everywhere!</p>
<?php } ?>
With this code we just say if it’s “home” do nothing, else show do this. You could add multiple conditions (exclusions) to this using the || OR operator as in previous examples.
Getting even more specific
One thing I hadn’t mentioned was that you could pass additional parameters to the conditional tags for even finer grained control. For example, instead of targeting are single post pages with “is_single”, you could actually target just one using any of these formats:
is_single(‘25′) // uses posts ID
is_single(‘Title of my post’) //uses the exact title of the post
is_single(‘title-of-my-post’) //uses the permalink of the post
is_single(array(25,’this title’,'this permalink’)) //uses when any of the 3 are true
You can use similar parameters for paged pages, template pages, categories, tags, etc. The official Wordpress Conditional Tag page in the Codex lists them all.
Force Wordpress Functions for Certain Conditions
Have you ever not wanted to add people to your blogroll because their link will display on EVERY SINGLE page of your WP powered site? That’s an easy hack with conditional tags, because you could hack your sidebar to display your blogroll ONLY on your homepage like this:
<? php if (is_home()) {
wp_list_bookmarks();
}
?>
You could modify this to display just about anything in the sidebar for whatever conditions you want. Let’s take this a bit further though – let’s say that maybe I want a special header or footer when certain conditions are met? You can do that too…
<? php if (is_home() || is_single() || is_page()) {
get_header();
}
elseif (is_category() || is_tag()) {
include (TEMPLATEPATH . '/header2.php');
}
elseif (is_404() || is_search())
include (TEMPLATEPATH . '/header3.php');
} ?>
Using that example code you could have as many different headers, footers, or sidebars as you wanted and you could include them for whatever conditions you specified. Just replace get_header with get_footer or get_sidebar, and edit the rest include the files you want.
Using Conditional Tags to Change Styles
So far my conditional tag examples have been to show you how code conditions to do this or include that. Another very simple (and powerful) was to use conditional tags is to just use them for coding style. You could have any element (paragraph, div, heading) change colors or font size or anything related to style when certain conditions are met.
Let’s take a very simple example, maybe your pages have content contained within one div like this:
<div id="content" class="main">
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<?php endwhile; ?>
</div>
In most themes, that main “div” is styled the same way on EVERY single page of the theme. Maybe you want it styled one way for your homepage, but another for paged pages, single posts, archives, etc. What you do in this case is to write different classes in your stylesheet for each and call them something like “single”, “archive”, and “paged” – and then you code it like this:
<div id="content" <?php if (is_home()) { ?> class="home"
<?php } elseif (is_page()) { ?> class="paged"
<?php } elseif (is_single()) ?> class="single"
<?php } elsif (is_archive()) { ?> class="archive" <?php } ?> >
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<?php endwhile; ?>
</div>
You use any variation of this to control any element of your theme at will under nearly any condition. This would also be a great way to control your Post title and meta and have it display different things on different parts of your Wordpress powered site.
Conclusion
I think mostly that the power of conditional tags is widely underutilized. Many of us just take for granted the way a theme works and looks, and forget that with just a few strokes of code and some imagination – we can change just about anything under the sun in our Wordpress theme! I’d like to see some comments on what kinds of things you’ve used conditional tags for in your Wordpress site (code samples welcomed!).
This post was submitted by John Pratt, who writes about how to Make Money Blogging. His latest post is How to Build a Wordpress Craigslist.

















