has_post_format() vs. get_post_format()

Edit

has_post_format() requires a string, $format, as the first parameter; which means that it can only be used to test for explicit post-format types:

if ( has_post_format( $format ) {
    // Current post has the $format post format;
    // do something
}

To determine if a post has any post format, use get_post_format(), which will return false if the current post does not have a post format assigned:

if ( false != get_post_format() ) {
    // Current post has a post format;
    // do something
}

Note that “standard” isn’t an actual post-format, but is rather a placeholder term for posts that do not have a post format assigned. Internally, WordPress returns false rather than post-format-standard, so, to query for the “standard” post-format type, you would just use if ( false == get_post_format() ).

Original

has_post_format() returns a BOOLEAN value, which is useful for conditionals, e.g.:

if ( ! has_post_format() ) {
     // I'm a standard-format post; do something
}

or

if ( has_post_format( array( 'gallery', 'image' ) ) {
     // I'm a gallery or image format post; do something
}

get_post_format() returns the string value of the current post format type, which is useful in several ways. One of the most powerful is to call different template part files based on post format, e.g.:

get_template_part( 'entry', get_post_format() )

Which will include, e.g. “entry-aside.php” for an aside format, or “entry.php” for a standard format.

Leave a Comment