I’ve been diving into WordPress’ new post-formats lately, and was able to quickly implement some of the new features into the site. I’ve really taken hold of two post formats: statuses (like a facebook status or a tweet) and links (to non-studionashvegas news, mostly from the WordPress world). But, I got to thinking – these statuses are great, but they’re taking away from blog content. I’d love to use them, but with every status or link I post I take off one post from my blog archive – the default setting is 10, and that’s not much in terms of real estate for blog posts.
I wondered if there was a way to manually tick off a counter when a standard post was used, but to skip the counter when a non-standard post format was used – that way there would always be 10 standard posts, but I could intersperse other content in without sacrificing a blog post on the front page.
My search took me to DigWP and an article they wrote a few years ago. In it, they talk of using a counter variable and adding one to it when the post is over:
<?php $i = 1; while (have_posts() && $i < 6) : the_post(); ?>
<h1><a href=”<?php the_permalink(); ?>”><?php the_title(); ?></a></h1>
<p>?php the_time(); ?></p>
<?php the_content(); ?>
<p><?php the_tags(); ?></p>
<?php $i++; endwhile; ?>
…other stuff…
<?php endif; ?>
I’ve hit the high points in bold. Basically they create a counter variable, set it to 1, and then require it to be less than a number (6 here). Once the post has run, it ticks the counter, then heads back to loop around again. If it’s at 6, it stops before it runs. Fair enough – that does it.
Next I turn to Lisa Sabin-Wilson’s reference on post formats (fantastic resource, by the way). It uses the “has_post_format” tag to check for specific post formats. Using that template, plus my own loop, I figured out how to do it. Be wary, though, this isn’t for the non-code literate; know your code or at least back up your stuff before you do this 🙂 (you’ve been warned).
Step 1: Define our Post Formats
add_theme_support( ‘post-formats’, array( ‘link’, ‘status’ ) );
The “add_theme_support” tag is used with the post-formats variable. I only want two types (link and status) but if I need to I can always add more later.
Step 2: Edit our Loop
This is where things get tricky. I’m going to paste my loop in its entirety and walk you through it (keep in mind this is written in HTML5-ish code):
<?php if (have_posts()) : ?> <?php $i = 1; while (have_posts() && $i < 11) : the_post(); ?>
<?php if ( has_post_format( ‘link’ )) { ?><div class=”clear”></div>
<article>
<div id=”post-<?php the_ID(); ?>” <?php post_class(); ?>>
<h2><?php the_title(); ?></h2>
<div class=”entry”>
<?php the_content(); ?>
</div>
<div class=”newsmeta”><a href=”<?php $key=”source”; echo get_post_meta($post->ID, $key, true); ?>”>Original Source</a></div>
</div>
</article>
<?php } elseif ( has_post_format( ‘status’)) { ?>
<div id=”post-<?php the_ID(); ?>” <?php post_class(); ?>>
<?php the_content(); ?>
</div>
<?php } else { ?><div class=”clear”></div>
<article>
<div id=”post-<?php the_ID(); ?>” <?php post_class(); ?>>
<h2><a href=”<?php the_permalink() ?>” rel=”bookmark” title=”Permanent Link to <?php the_title_attribute(); ?>”><?php the_title(); ?></a></h2>
<div class=”entry”>
<?php the_post_thumbnail( ‘blogthumb’ ); ?>
<?php the_excerpt(); ?>
<?php $i++; ?>
</div>
</div>
</article>
<?php } ?>
<?php endwhile; ?>…other stuff…
<?php endif; ?>
Confusing yet? I’ve broken the new stuff into two colors: the counter and the post-formats code. The red is the same counter code used in DigWP’s code. The lavendar/lilac/light-purple is the post-format code. Notice how I start it off with the has_post_format tag:
<?php if ( has_post_format( ‘link’ )) { ?>
That checks to see what post format I have and displays the loop stuff accordingly. There’s also an elseif later on that checks for the status, then a final else to display standard (or other, undefined post types) later on. Then, it displays the normal article stuff.
The key to note is that I’ve included the $i++ under the excerpt on the “standard” articles – this ticks the counter before it returns to the top to do it all over again.
One Caveat I’ve Noticed…
The WordPress Default “Posts Per Page” option will over-ride the counter every time. So, I ended up setting mine to 20. I don’t plan on having more than a few “other types of posts” so this is a good number.
How I Can Improve
I’d rather hook this into the main “posts per page” option, but for right now I’m really not sure on how to do that – this will work for now, however. And, it gives me the control I need and allows me to utilize those new post formats without sacrificing my real content.