Since you need a specific HTML structure let’s keep things clean and do what The Codex suggests – break it into template parts based on the post format. Let’s create a subfolder in your theme to store these post formats, maybe POST_TYPE_SLUG-formats
so it’s clear what is in this folder ( Replace the uppercase with your actual post type slug ). Then we can store file in this folder like format-aside.php
with all the markup you need.
Finally, our loop would look like this:
<?php
if( have_posts() ) {
while( have_posts() ) {
the_post();
$format = get_post_format();
$format = ( FALSE !== $format ) ? $format : 'default';
get_template_part( 'POST_TYPE_SLUG-formats/format', $format );
}
}
?>
IF our post type was just the built-in post
we would have a subfolder in our theme called post-formats
and inside a few format files like: post-formats/format-aside.php
and post-formats/format-chat.php
. In the case that, for whatever reason get_post_format()
fails and returns false
it’s probably a good idea to add post-formats/format-default.php
as a fallback.
In your format file you have full access to the loop functions like the_content()
and the_title()
.