WordPress Custom Templates: The EASY and Most Logical Method
What the hell?
You may either be thinking oh yeah I know about that, or you're thinking what the hell is he talking about. Well, I'm not the first to know about this, and maybe the last. Antonio pointed this out to me today, and it was a lifesaver for me. I don't know how I missed it, in my quite extensive research of 3.0, and the reading I did of the source code of core WordPress files that have new 3.0 additions.
There are now page-slug.php and page-id.php Templates
No more need really to set up custom page templates where you do something like:
<?php /* Template Name: SomePage */ ?>
Nope, and wow that's just great news. Just when I was freaking out, thinking I was insane (which is true but not always my fault) because my f***ing local setup and my live site setup of the theme I'm making, with the exact matching settings everywhere, would not for the life of me work. By that, I mean the custom page template I had created for using on a specific page, did not work in both places. Only on the local site. It drove me nuts for the better part of today.
Antonio happens to mention to me this page-slug.php thing, right before I was going to try submitting this as a bug in 3.0 (I tried everything srsly, it had to be a bug, maybe is idk). I don't care if it is a bug now though to be honest, what with this new page-slug thing, which resolved my dilemma.
I had heard of the custom single templates before, but didn't know that page had it too. It was one of those AHA! moments more or less, or something, maybe, not quite, but you know what I mean.
What? There's custom single templates too?
Yes! Isn't it great!
Here is the WordPress template hierarchy which lists all the new, (or not-so-new) custom templates which you can create, and easily use, as automatic pages, no need to do that bit of code mentioned above, just create a page with a slug or id # which is the same as in your file names.
Displaying Custom Templates Up To 3.0
| Template File Name | Description |
|---|---|
| single-post_type.php (added in 3.0) |
If you use custom post types, WordPress will look for the file single-post_type where post_type could be whatever you've defined (ie: music, videos, languages etc.) |
| author-nicename.php (added in 3.0) |
If the author's nice name were bobbybigballs, WordPress would look for author-bobbybigballs.php. |
| author-id.php (added in 3.0) |
If the author's ID were 6, WordPress would look for author-6.php. |
| taxonomy-taxonomy-term.php (added in 3.0) |
WordPress would look for taxonomy-sometax-someterm.php. |
| taxonomy-taxonomy.php (added in 3.0) |
If the taxonomy were sometax, WordPress would look for taxonomy-sometax.php. |
| page-slug.php (added in 2.9) |
If the page slug is recent-news, WordPress will look to use the file page-recent-news.php. |
| page-ID.php (added in 2.9) |
If the page ID is 6 or 4602, WordPress will look to use the file page-6.php or page-4602.php, respectively. |
| category-slug.php (added in 2.9) |
If the category's slug were news, WordPress would look for category-news.php. |
| category-id.php (added in 2.9) |
If the category's ID were 6, WordPress would look for category-6.php. |
| tag-id.php (added in 2.9) |
If the tag's ID were 6, WordPress would look for tag-6.php. |
| tag-slug.php (added in 2.3) |
If the tag's slug were sometag, WordPress would look for tag-sometag.php |
I just realized I didn't talk about other custom single templates. I will have a crack at this before I leave for work, and will fix any wrongs when I get there, ha! Sorry, poor editorial management. I'm a designer, developer, and author. I can't always do it all, correctly and seamless.
Here goes:
<?php
$ptype_obj = $wp_query->get_queried_object();
$ptype_ID = $ptype_obj->ID;
$ptype_title = $ptype_obj->post_title;
$ptype_slug = $ptype_obj->post_name;
if (file_exists(TEMPLATEPATH.'/single-'.$ptype_slug.'.php')) {
include(TEMPLATEPATH.'/single-'.$ptype_slug.'.php');
}
?>
What I am going for with this code is a way to use single.php templates, for ANY post type or.... Wait, that's really all there is to this isn't there hahaha. I guess this code is not necessary, since the single-post_type.php takes care of that right? So, for those that would like to check this code out or just see me have an AHA! moment while writing this update to a post that did not need one, here ya go.
* Note: The above code snippet is not something I've tested, I just wrote it up in the past 5 minutes to throw in here lol. If you try it somehow in the next hour, before I can get a chance to at work let me know if it works. I would just use the single-post_type.php that I've mentioned in the table above, before my nonesense. It actually works, and is built-in.
Wow, that is Awesome!
Yes, yes it is. I said it was good stuff didn't I. Well, I hope we all learned something today here folks.
That knowing is half the battle.- GIJoe
OH, I almost forgot
How's about the nifty stripey table I got going on here huh? eh? I'll have you know, cause I know you care so much, making that stripey table is something I learned to do in the JavaScript Live online course I am so thankful I signed up for at Sitepoint.com run by Kevin Yank. Thanks Kev, for helping me learn to make cool stuff! (even if jQuery can do it much simpler :P) but still, thanks a ton! :D






User Comments
( ADD YOURS )Hristo August 6
Thanks a lot for the tutorial! I'm building a new site and I was frowning at the idea of having to make custom templates for posts and pages by adding the comment header for each and then select them from the drop down menu for each new piece of content...
This makes it so much easier!
Jared August 7
I know what you mean. When I found out about doing it this way, it made my day so much better. I have been making custom templates this way ever since and haven't had one complaint or issue in doing so.
Glad you found it as useful as I did.
EbookLover October 6
Wow, I didn't notice this feature before. Thank you very much. My life should be easier now :)
Nancy Boyle November 30
Jared, Thanks so much! I am trying to learn how to use CPT. Meanwhile, I have been using the plugins Custom Post Type UI and Custom Post Type Archives. The Archives plugin set up has been tricky for me. I will try to use your ideas to configure it. Saw your link on the Boston WP meetup group.
Thanks, Nancy
Roger June 29
God Bless WordPress, and this blog! I only wish I had switched from Blogspot months ago, would have saved me all the headache of trying to customize...
Trackbacks