Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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!

share|improve this question
add comment

4 Answers

up vote 6 down vote accepted

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

share|improve this answer
add comment

You can use has_post_format or you could use get_post_format. Lisa Sabin-Wilson has a good article using them called WordPress 3.1 Post Formats Reference.

has_post_format example: <?php if ( has_post_format( 'video' )) { echo 'this is the video format'; } ?>

get_post_format example: <?php $format = get_post_format( $post_id ); ?>

share|improve this answer
add comment

While all the methods above will work there is a much cleaner way to do it as shown by Dougal Campbell

while ( the_loop() ):
    get_template_part( 'format', get_post_format() );
endwhile;

Going with that method will kill at the conditional statements and mean you don't have to add more if you add support for another post format. All you'll have to do is add the template file.

share|improve this answer
3  
How is that different from the code in my answer? –  Bainternet Mar 11 '11 at 18:44
    
Good call, it's not really, guess I read through the other answers too fast and only caught the conditional stuff assuming the rest was more of the same. So do I delete or just leave it b/c of the link to the longer blog post? –  curtismchale Mar 11 '11 at 22:15
add comment

Just to add a brief update, if you want to query only for the "standard" post format (the default, that is), you have to query for all posts NOT in other post formats. This is because WordPress stores 'false' rather than 'post-format-standard' for standard posts.

Kind of irritating, but that's the only way I was able to do it.

Here's the 'tax_query' I used to only return standard posts:

'tax_query' => array(
    array(
        'taxonomy' => 'post_format',
        'field' => 'slug',
        'terms' => array('post-format-aside', 'post-format-gallery', 'post-format-link', 'post-format-image', 'post-format-quote', 'post-format-status', 'post-format-audio', 'post-format-chat', 'post-format-video'),
        'operator' => 'NOT IN'
    )
)

Also, note that the taxonomy name is "post_format" not "post-format" as it was written above. I tried to correct the above example, but couldn't make a one-character edit.

share|improve this answer
    
This is a known bug and there's a patch in trac to fix it some day :) core.trac.wordpress.org/ticket/16149 –  mrwweb Apr 11 '12 at 22:07
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.