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.
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:To determine if a post has any post format, use
get_post_format()
, which will returnfalse
if the current post does not have a post format assigned: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 thanpost-format-standard
, so, to query for the “standard” post-format type, you would just useif ( false == get_post_format() )
.Original
has_post_format()
returns a BOOLEAN value, which is useful for conditionals, e.g.:or
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.:Which will include, e.g. “entry-aside.php” for an aside format, or “entry.php” for a standard format.
The following part is not correct, I have created a ticket to request this enhancement.
has_post_format()
is more flexible because it builds uponhas_term()
, which builds uponis_object_in_term()
. This means you can pass an array of post formats and it will returntrue
if the posts has one of these formats.The original specification ticket already mentioned both
get_post_format()
andhas_post_format()
, maybe because it builds upon the taxonomy system which also has both functions?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:
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.