I just finished tweaking the VoodooEmpire theme to use the new WordPress Post Formats. I’m going to share my experiences. I actually had a hard time figuring out exactly how to accomplish what I was after. After WordPress 3.0 was released, I converted the theme to use get template part with loop.php. I only had loop.php, which was a big monster loop with lots of conditionals. It was complicated, but nobody had to be able to edit it. That’s the point of get template part. Now Post Formats come out. At first it seemed simple, but once I tried to implement it I realized it was going to be a bit tougher. I wanted to use my loop.php and I wanted to use formats. But I wanted child themers to be able to override any of the loops or formats easily with get template part. So, here’s what I did. This was my first attempt, maybe I’m over complicating things, maybe you have a better way, I’m always open to suggestion! Let’s take a look.
First off, let’s look at the loop. Like every other theme, my loops all used to be in their respective templates. I had a lot of specialized templates to make my theme useful for many plugins. Feed WordPress, eShop, Custom Post Types, etc. That’s a lot of loops. So when WordPress 3.0 came out, I jumped on the get template part feature. I combined all my loops, used a lot of conditionals to get things just how I wanted them for everything. It was perfect! It was also complicated. But to me, that didn’t matter. Nobody needed to edit my loop.php. That’s the point of child themes. Each and every template had a call to get template part for loop.php relating to the template, like this:
<?php get_template_part( 'loop', 'index' ); ?>
For each of my templates, the ‘index’ word is changed so that customized loops can be used in child themes by calling loop-shop.php, loop-index.php, etc. So that worked great, maximum child theme-ability for users.
But soon WordPress 3.1 is coming out. And that caused me a dilemma. I wanted to incorporate post formats, and also give theme users the ability to override those with get template part. So in the end users should be able to put loop-whatever.php or format-whatever.php into their child theme and be able to override any loop or format template with their own. Here’s my attempt to do that.
I kept all my templates calling get template part pointing at the loop files. For instance, here is my index.php in it’s entirety.
<?php get_header(); ?> <div id="content"> <?php get_sidebar('top'); ?> <?php get_template_part( 'loop', 'index' ); ?> </div> <?php get_sidebar(); ?> <?php get_footer(); ?>
See? So Simple I could cry! All my templates are that simple, they just call in various sidebars, and various template parts based on loop.php. Now with the new post formats, that didn’t change at all. I’ll be honest, I don’t know if I was over-thinking this or what, but I had a hell of a time conceptualizing how to get this all done. But anyway, onto the next step in my thought process.
Loop.php used to contain all my loop elements for all templates. I had originally thought about just using a bunch of conditionals to include post formats. But I don’t think that would allow any control through child themes? So what I did was offload to Post Formats from loop.php. In my mind at least, this allows people to child theme both loops and formats using get template part. Here is my loop.php now:
<?php if (have_posts()) : ?> <?php while (have_posts()) : the_post(); ?> <div id="post-<?php the_ID(); ?>"> <?php $format = get_post_format(); if ( false === $format ) $format = 'standard'; get_template_part( 'format', $format ); ?> </div> <?php endwhile; ?> <div class="navigation"> <div class="alignleft"><?php next_posts_link(__('« Older Entries')); ?></div> <div class="alignright"><?php previous_posts_link(__('Newer Entries »')); ?></div> </div> <?php else : ?> <div class="post" id="post-<?php the_ID(); ?>"> <?php theme_else(); ?> </div> <?php endif; ?>
We can see that this template opens and closes the loop, but removes the actual content between the opening and closing divs and replaces it with:
<?php $format = get_post_format(); if ( false === $format ) $format = 'standard'; get_template_part( 'format', $format ); ?>
This little block of code offloads to the proper format template. Just a little note with get post format, if the post doesn’t get one of the formats it returns false. All my normal blog posts will not have a format assigned. But if they don’t have a format, they won’t pick up a format template, thus making it harder for a child theme to style them. This block of code says to check the post format, if it’s false, assign it to standard, then go get the proper format template.
Now what I finally did was make a template called format-standard.php and put my original giant loop in there for my normal blog posts. I then made another template called format.php which used a bunch of conditionals to handle all the other post formats. Again, I could have used individual format-whatever.php for each format, but one file is easier for me. The main point was to make format-whatever.php available for child theme use, with whatever being restricted to current formats.
So let’s sum up my thought process. All my various templates call get template part based on loop.php. This makes sure any loop can be customized in a child theme via loop-whatever.php. Next up my loop.php opens and closes the loop but no content in there. Instead we use a block of code to assign false formats to ‘standard’ format and then use get template part to call to the appropriate format. I made a format-standard.php to handle my normal loop, and format.php to handle the other formats. This gives child themers format-whatever.php to customize formats with.
That’s it for my end. Everything works perfectly for me, and lots of options are available to child themers! The only stumbling block I can think of in my mind is if a user wishes to make a custom loop file which also has custom format styling available. They will need to include the above post format code in their own template to have the custom loop and the custom formats, or they will need to style all formats in their templates. But I think explaining that in a readme.txt file should be sufficient.
What do you think? Will this work well? Better solutions? I know very little code, and have very little experience, so this is just my first try at coding through my own thought process.
<blockquote cite="I know very little code, and have very little experience, so this is just my first try at coding through my own thought process."
That part is great, made me laugh, really. Well bud this post/tut will be helpful to many of WordPress users, especially very soon when 3.1 is out. Cool stuff, I like the simplicity.
Cheers,
Emil
Thanks for taking the time to comment! I hope somebody finds the tutorial useful. This process works out great for me, so I’m hoping it will work for others too. Or at least help someone think of their own great idea to share.
Oh – picking up the post format type and plugging that straight into
get_template part
is so simple and yet so elegant! Saves all of those switch/case statements that I’s started writing. It also deals very nicely with a situation where one of the single post format template files has been accidentally deleted. Without that fallback, WP will use index.php – which has the potential to cause a lot of confusion (well, it confused me the first time it happened…)Nice one!
Thanks very much for taking the time to comment. I appreciate it. Especially letting me know that I may have gotten something right! I just hope I help some people, either directly….or by giving a boost to someone else’s thought process. There’s a whole lot of creative stuff that can be done with template parts and post formats, etc…
This is a great write-up – thanks for documenting the process so well. I’ve been looking for an elegant way to combine get_template_part() with post formats and I think I’ll be using an approach like this myself. I like to pull the actual content in using action hooks as they provide great flexibility, but when you mix template functions, post formats, and action hooks, the code starts to get nastily complicated very quickly. This looks like a very promising method for keeping things as simple as possible.
Thank you for the positive feedback. I agree things can get quite complicated. I’m not even sure how many times I went over my options trying to decide how I wanted to approach this. WordPress keeps giving us more options, which is great. We have so many different ways to implement things, it can be tough to pick sometimes.
Hi,
How to display on the homepage only a standard post format?
What do you mean exactly? You want to not have any special formatting on the home page? Or you only want posts assigned as ‘standard’ to show up on the homepage? To do that, you would need to run a query including only the standard format. Formats are considered taxonomies. http://codex.wordpress.org/Function_Reference/WP_Query#Taxonomy_Parameters
Could this same logic be applied for instance on single.php? I see here your example works for index.php. I am curious if that can be applied to single.php when dealing with post formats while styling them differently.
Are you just looking for specific single templates to be used for the formats? There’s already a naming convention for template files taxonomy-post_format-post-format-link.php for handling formats. Dunno if that helps at all?
Thanks for this tutorial, worked perfectly for me : )
Cool, I’m glad you found it useful!
Hi jsut wondering what the fucntion is? Thanks
Thats is php theme_else(); function (sorry stripped out from original question).
Oh sorry, that doesn’t really do anything you need for this tutorial. What I do in my themes, instead of putting all my ‘posts not found’ info at the bottom of each template, is put it in a function in functions.php, then I can just call it like that. Fo rinstance in my new theme I’m making, I put this in functions.php:
And then at the bottom of each template I make, instead of putting that code over and over, I can just call voodoo_else();
Hi! I was researching about this stuff ” ” and i ran on this site. can you please tell me how to edit my (image post)(title)(contents). I mean instead of this looping how can i just put
in my index page? *this is just an example. i hope you got what i mean Im using 3.1 thanks you very much!
aw is removes the text that has enclosed in php tags. I ‘ll repost it..
I was researching about this stuff ” ” and i ran on this site. can you please tell me how to edit my (image post)(title)(contents). I mean instead of this looping how can i just put
in my index page? *this is just an example. i hope you got what i mean Im using 3.1 thanks you very much!
Hi! I was researching about this stuff ” get_template_part( ‘content’, get_post_format() ); ” and i ran on this site. can you please tell me how to edit my (image post)(title)(contents). I mean instead of this looping how can i just put
< thumbnails < p title in my index page? *this is just an example. i hope you got what i mean Im using 3.1 thanks you very much!
I put instructions down on my comment form. If you need to post code on this website, you need to wrap the code in language tags, inside squalre brackets… like php, html, css, etc. Then It’ll get displayed all pretty like. Anyway, I’m not exactly sure what you are asking. So you are editing a theme? What theme? Does it already use get_template_part, and you want to change that behaviour? If so, I’d recommend still using the get_template_part, and I’ll help you sort throuigh how to do your changes. BUt if you can help me understand a bit better what exactly you have, and what you are trying to do, I might be able to help. Also, if you need to post a lot of code, like a whole template, go to http://pastebin.com and copy it there, save it, and then just give me the lik to the saved page.