Skip to content

Instantly share code, notes, and snippets.

@cassler
Last active August 29, 2015 13:58
Show Gist options
  • Save cassler/10061764 to your computer and use it in GitHub Desktop.
Save cassler/10061764 to your computer and use it in GitHub Desktop.
Post Format Detecting Loop

Use Details

You'll want to start by creating a file ```/content/loop.php`` - will be your default or "standard" format loop. For additional post format support, you'll need to add a loop file to match. For example, if your theme supports the post formats 'aside', 'video', and 'audio' then you should have:

  • /content/loop.php : Standard/Default View
  • /content/loop-aside.php : View for Aside Post Format
  • /content/loop-video.php : View for Video Post Format
  • /content/loop-audio.php : View for Audio Post Format

As you cycle through your loop, the correct template part will be rendered for each post format.

<?php
## Super Dynamic Loop Shenanigans
if ( have_posts() ) :
while ( have_posts() ) : the_post();
## Asign variable to post format. We'll use this to avoid if statements.
$format = get_post_format();
## If format is false, set it to 'standard' instead. This assures that the loop will
## Always be displayed regardless of post format settings.
if ( false === $format ) { $format = 'standard'; }
## Use our format variable to choose a loop
get_template_part( "content/loop-$format" );
## Now do this once we've displayed our posts.
## Page navigation etc.
endwhile;
## Wait, we didn't find anything.
## No posts found
else:
endif;
## End Super Awesome Loop Shenanigans
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment