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.

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.

share|improve this question
add comment

3 Answers

up vote 8 down vote accepted

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.

share|improve this answer
    
The get_template_part is really smart! –  kaiser Apr 8 '11 at 18:03
    
Bennet - complete forgot to mark your A as solution. Btw: All As were upvoted :) –  kaiser Apr 12 '11 at 19:45
1  
if ( ! has_post_format() ) {} returns a warning (as of 3.5-RC1 at least) since the 1st (required) argument is missing. codex.wordpress.org/Function_Reference/has_post_format –  glueckpress Nov 29 '12 at 11:14
    
@glueckpress thanks for that; answer updated. –  Chip Bennett Nov 29 '12 at 12:32
add comment

The following part is not correct, I have created a ticket to request this enhancement.

has_post_format() is more flexible because it builds upon has_term(), which builds upon is_object_in_term(). This means you can pass an array of post formats and it will return true if the posts has one of these formats.

if ( has_post_format( array( 'aside', 'video' ) ) {
    // It's an aside or a video
}

The original specification ticket already mentioned both get_post_format() and has_post_format(), maybe because it builds upon the taxonomy system which also has both functions?

share|improve this answer
    
Oh, definitely, this further extends what you can do here by allowing you to check on specific formats in order to get your true/false answer back. –  Drew Gourley Apr 8 '11 at 12:03
    
Makes me think if it would make sense to simply use the is_object_in_term() function instead. –  kaiser Apr 8 '11 at 18:11
1  
@Jan Fabry has_post_format() expects a string as first parameter. An array will fail. –  toscho May 4 '11 at 12:45
1  
@toscho: Darn, I knew I should have tested that instead of just glancing at the code. Then it is inconsistent with other has_* functions - I have created a ticket for it. –  Jan Fabry May 4 '11 at 13:07
2  
@Jan Fabry That’s a very cool way to fix your answer. :) –  toscho May 4 '11 at 14:32
show 3 more comments

Simple, has_post_format() returns a true/false (Boolean) value which is useful in IF statements, while get_post_format() returns the post format, if one exists, and probably NULL or false if one isn't there. Using Boolean values is a good clean way of making sure your conditions always behave the way you were expecting and the has_post_format() function allows for nice easy short conditions:

if ( has_post_format() ) {
  //yes we do
} else {
  //no we do not
}

if ( !has_post_format() ) {
  //no we do not
} else {
  //yes we do
}

Also, this just falls in line with other existing WordPress functionality. While your option B gets things done, it requires a bit more specialized knowledge than maybe the slightly above-average WordPress user is familiar with.

share|improve this answer
    
Reminds me of the get_adjacent_post and next_post_link stuff. –  kaiser Apr 8 '11 at 18:12
add comment

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.