I just stepped into the concept of post formats and was wondering why there are two out of 3 functions from the post format “API” offer absolutely the same functionality. Consider the following two concepts (A vs. B):

if ( have_posts() )
{
    while ( have_posts() )
    {
        the_post();

        // A) has_post_format
        if ( has_post_format('format') )
        {
            the_excerpt(); // some special formating
        }

        // VERSUS:

        // B) 
        if ( get_post_format( $GLOBALS['post']->ID ) == 'format' )
        {
            the_excerpt(); // some special formating
        }

    } // endwhile;
} // endif;

Could someone please explain me why there are those two functions instead only ex. get_post_format? If you could offer me some examples of situations where the one isn’t capable of something the other function can do, I’d be special happy and +1 it.

3 s
3

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.

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *