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.

As suggested on this site, I am using this code to style an excerpt if no post format is selected/if it is a standard post.

<?php
$format = get_post_format();
if ( false === $format )
{
echo 'This is a default post';
the_excerpt();
}
?>

However, it seems there must be a better way to include it with one PHP statement... the rest of which follows this pattern:

if ( has_post_format( 'video' )) {
    ?>
    A VIDEO POST
    <?php the_content();
}
if ( has_post_format( 'audio' )) {
    ?>
    AN AUDIO POST
    <?php the_content();
}

Anybody know how to include the original code in this same if statement? If, then...

share|improve this question
    

3 Answers 3

up vote 0 down vote accepted

I'm not sure I understand. Do you just want to join all the if-statements together? If that's the case you could use elseif, like so:

<?php
$format = get_post_format();
if ( false === $format ) {
echo 'This is a default post';
the_excerpt();
} elseif ( has_post_format( 'video' )) { ?>
   A VIDEO POST
    <?php the_content();
} elseif ( has_post_format( 'audio' )) { ?>
    AN AUDIO POST
    <?php the_content();
}
?>
share|improve this answer

I prefer using template-part files, with get_template_part() and get_post_format():

<?php get_template_part( 'post', get_post_format() ); ?>

This method will then look for, e.g. post-gallery.php, and fall back to post.php if either post-gallery.php doesn't exist, or if get_post_format() returns false.

Then, for each Post Format type, you simply create post-{post-format-type}.php, and you're good to go.

share|improve this answer

Use a switchcase instead.

switch ( get_post_format() ) {

case 'video':
  // video post code
  break;

case 'audio':
  // audio post code
  break;

default:
  // default post code
  break;

}
share|improve this answer

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.