我刚谈到post格式的概念,我想知道为什么post格式的3个函数中有两个提供了完全相同的功能。考虑以下两个概念(A和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;谁能解释一下为什么会有这两种功能而不是只有前任。get_post_format?如果你能给我举几个例子,说明一个函数不能做另一个函数所能做的事情,我会很高兴,并且+1它。
发布于 2011-04-08 11:30:53
简单地说,has_post_format()返回一个真/假(布尔值)值,这个值在IF语句中很有用,而get_post_format()返回post格式(如果存在的话),如果不存在,则返回空或假。使用布尔值是一种很好的、干净的方法,可以确保条件始终按照预期的方式运行,而has_post_format()函数允许很好的简单的短条件:
if ( has_post_format() ) {
//yes we do
} else {
//no we do not
}
if ( !has_post_format() ) {
//no we do not
} else {
//yes we do
}而且,这与其他现有的WordPress功能是一致的。虽然您的选项B可以完成任务,但它需要比稍微高于平均水平的WordPress用户所熟悉的更多的专业知识。
https://wordpress.stackexchange.com/questions/14257
复制相似问题