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.