Wordpress Templates and Meta Information
June 12th, 2005
Maybe I am slow, but I just discovered some pretty sweet features of Wordpress today. The first involves themes and the templating system and the second involves custom fields and meta information. I figured it is very possible that others have not stumbled across these features so I thought I would share them.
The Templating Trick
This is so easy that it is barely a trick and probably many already know this but…ever notice that when you go to write a page it has the page template drop down? I don’t know about you, but I was wondering how in the heck that list was populated. I mean, I figured out that if you created an archives page and used the archives template from the drop down that it would use the archives.php file in your theme folder. This was nice because then you can write all your magical php functions to make your archives pretty. I also did this with my links page. The funny thing is, that I never thought twice about the lines at the top of those files.
/*
Template Name: Archives
*/
It dawned on me today that Wordpress is using those lines to populate the template drop down when you write a page. I was like, “No, that is to stinkin easy!” Really, I was like that. I laughed giddilly like a little schoolboy because that is what geeks do when they figure out something that they think is cool. Ok, so maybe you already know this, but I’m sure someone out there doesn’t, in which case, they too are probably laughing like a schoolboy. Go ahead, try it out. Just create a new file in your theme directory called test.php. Copy and paste the source code from your archives.php or links.php file into test.php. The last step is to change the code in the test.php file where it says Template Name: Archives to Template Name: Test. The final step is to open up your admin area (which you hopefully have themed with my buddy Steve Smith’s Tiger Admin) and go to Write Page. If you check out the Page Template drop down you will notice the new test template you just created is now there.
Now you can create any template you want by creating a new php file in your theme directory, adding the Template Name code at the top, and selecting your new template as the Page Template when you create the page.
The Meta Information
Even cooler than the simple templating trick is the meta information. For a new site that my friend and I are creating, I wanted to store a thumbnail and a large image for each post. The site will be featuring photos and I wanted a way to show just the photo or just the thumbnail or just the photo and the description. I headed over to the wordpress codex to see if I could find a way to accomplish that. The ‘Add New Custom’ field section in the advanced editing of a post has been intriguing me for a while so I decided to check it out. I read through the article on using custom fields and I had a light bulb go off.
For each post on the new site I am developing, I enter the title, a description, and I create two new custom fields, one named ‘main’ and another named ‘thumb.’ The value of these fields are the respective full size and thumbnail image names for the particular post. Now each post has meta information on the main image and the thumbnail image. So how do I show them? I ran across the get_post_custom() function and new I was in business. This function accepts an article ID as a parameter and returns all the meta information for the post in an array. I wrote the function below to create the URL for whichever type image I pass as a parameter, through it in a few spots in my theme pages and I was good to go.
<?phpfunction getPhotoURL($id, $type) {$meta = get_post_custom($id);$url = get_bloginfo('siteurl') . '/images/';if ($type == 'main') {$url .= $meta['main'][0]; // main image name} else {$url .= $meta['thumb'][0]; // thumbnail image name}return $url;}?>
One thing worth noting is that the above function has to be run in ‘The Loop‘ to work correctly. I’ll have a better write up on how to use custom fields after my friend and I launch our new site. Until then hope this helps somebody out.
Thanks for taking the time to blog about your WP coding adventures.
You took the time to do it reasonably well.
Nice tips.
very intresting stuff I hope you don’t mind I am adding you to my site http://www.layouts.com
Ahhh yes monsier, but the challenge I am currently pontificating upon is how to combine those two juicy pieces of knowledge. I’m putting my resume into a new format online.
-I see using the custom fields to store my employment history (or freelance project) start date / end date as useful so that the top level page could show just that info and the actual post show the full details of the position, etc.
-Pages offer better navigation structure and templating to suit this as the content is not subject to much change…just add a new sub-page whenever I change jobs. But I can’t use the custom fields on pages, and would have to hand-build the navigation elements if using pages, whereas posts tracked by category templates are done for me.
-Outstanding issue: Are there post-level templates I can use??!?!
As you can see my brain is a jumble of these thoughts currently, in the learning of wordpress state that it is. If you can offer some thoughts it would be appreciated.
Thanks for the trick. It works perfect!
Regards