“post-format” code snippets list [closed]

Using get_template_part() with Post Formats

This is a really handy little snippet to use when you want to change the output format for each post format.

if ( have_posts() ) :
     while ( have_posts() ) : the_post();

        // Standard is the default template for posts with no post format
        // As the formats doesn't contain  it, but the function returns false
        // We add it as fallback
        get_template_part( 'format', ! get_post_format() ? 'standard' : get_post_format() );

        endwhile;
endif;

In your theme folder create a file for each post format, like so:

  • format-standard.php (this is the default template)
  • format-video.php (this file would be used for posts with video formats)
  • format-audio.php (this is used for audio formats)
  • etc, etc

This snippet would be used in your single.php and/or blog archive templates to control the output format of each post format. Doing it like this really helps to keep your template files clean and easy to read.

This is described more in-depth in the Post Formats tutorial on WP Roots.com