has_post_format() vs. get_post_format()

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.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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.

Method 2

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?

Method 3

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.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x