Display/query post formats

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

tech