Display/query post formats

WP 3.1 has everyone excited, if somewhat confused, about post formats.

Enabling new post formats

The issue of enabling post formats has been covered at length. It’s as simple as adding this line to functions.php: add_theme_support( 'post-formats', array( 'aside', 'gallery' ) );

Displaying Posts of a certain Format

But the issue of displaying these posts has hardly been covered at all. Lets document the process of displaying/querying posts of a certain format using an example:

Let’s say we want to put a Twitter-like status update in the sidebar. It’s easy to enable the status post format, but how to I actually query for those posts to make them show up in the sidebar?

I’ve done quite a bit of searching and haven’t found an answer to this problem, all are welcome to contribute. If we come up with a good answer, I think it would be the first to document this issue.

With thanks!

4 s
4

You have a lot of options for displaying using the “Post Formats” feature:

for example in an index.php loop you can decide what to show based on the post format
using has_post_format() function :

        if ( has_post_format( 'aside' )) {
            echo the_content();
        }

        elseif ( has_post_format( 'chat' )) {
            echo '<h3>';
            echo the_title();
            echo '</h3>';
            echo the_content();
        }

        elseif ( has_post_format( 'gallery' )) {
            echo '<h3>';
            echo the_title();
            echo '</h3>';
            echo the_content();
        }

        elseif ( has_post_format( 'image' )) {
            echo '<h3>';
            echo the_title();
            echo '</h3>';
            echo the_post_thumbnail('medium');
            echo the_content();
        }

        elseif ( has_post_format( 'link' )) {
            echo '<h3>';
            echo the_title();
            echo '</h3>';
            echo the_content();
        }

        elseif ( has_post_format( 'quote' )) {
            echo the_content();
        }

        elseif ( has_post_format( 'status' )) {
            echo the_content();
        }

        elseif ( has_post_format( 'video' )) {
            echo '<h3>';
            echo the_title();
            echo '</h3>';
            echo the_content();
        }

        elseif ( has_post_format( 'audio' )) {
            echo '<h3>';
            echo the_title();
            echo '</h3>';
            echo the_content();
        }

        else {
            echo '<h3>';
            echo the_title();
            echo '</h3>';
            echo the_content();
        }

Using get_template_part() and get_post_format() to get a defferent loop based on the format, This is assuming that you have made a format loop.php (say format-status.php) file for each format used in your theme so you just call it :

get_template_part( 'format', get_post_format() );

And you can also query posts based on their format:

$args = array(
                'tax_query' => array(
                    array(
                        'taxonomy' => 'post-format',
                        'field' => 'slug',
                        'terms' => array( 'post-format-quote' )
                    )
                )
            )
            $query = new WP_Query( $args );

and last (for now) you can use “post_class();” function to style based on CSS

<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>

this will output something like:

<div id="post-id" class=”post format-status”>

Hope this helps getting started

Leave a Comment