首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >has_post_format()与get_post_format()

has_post_format()与get_post_format()
EN

WordPress Development用户
提问于 2011-04-08 10:57:43
回答 1查看 10K关注 0票数 10

我刚谈到post格式的概念,我想知道为什么post格式的3个函数中有两个提供了完全相同的功能。考虑以下两个概念(A和B):

代码语言:javascript
复制
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它。

EN

回答 1

WordPress Development用户

发布于 2011-04-08 11:30:53

简单地说,has_post_format()返回一个真/假(布尔值)值,这个值在IF语句中很有用,而get_post_format()返回post格式(如果存在的话),如果不存在,则返回空或假。使用布尔值是一种很好的、干净的方法,可以确保条件始终按照预期的方式运行,而has_post_format()函数允许很好的简单的短条件:

代码语言:javascript
复制
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用户所熟悉的更多的专业知识。

票数 3
EN
页面原文内容由WordPress Development提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://wordpress.stackexchange.com/questions/14257

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档