当前浏览:首页 / WordPress函数 / is_single()

is_single( int|string|int[]|string[] $post = '' )

是否为现有的文章单页

is 条件判断more...

single


描述

适用于任何文章类型,附件和页面(page)除外

如果指定了$post参数,此函数将另外检查查询是否针对指定的某个文章。

更多类似的主题函数信息,请查看主题开发手册中的条件标签文章。

另见


参数

$post

(int|string|int[]|string[]) (可选) 要检查的文章ID、title、slug或其数组。

默认值: ''


返回

(bool) 查询是否针对现有的文章单页。


说明

  • 另见:is_singular()
  • 尽管is_single()对于附件来说通常会返回true,但这种行为不应该被依赖。有可能$is_page$is_attachment同时为true,在这种情况下,$is_single将为false。出于这个原因,如果你想连同附件一起判断,应该使用is_attachment() || is_single(),如果你还想连页面也一起判断,则使用is_singular()


源码

查看源码 官方文档


更新日志

版本描述
1.5.0开始引入

使用示例

  • 示例1
    // Run code only for Single post page
    if ( is_single() && 'post' == get_post_type() ) {
      
    }
    
    //if it's not a specific post-type
    if ( is_single() && 'portfolio' != get_post_type() ) {
      
    }
    
  • 示例2
    is_single();
    // When any single Post page is being displayed.
    
    is_single('17');
    // When Post 17 (ID) is being displayed.
    
    is_single(17);
    // When Post 17 (ID) is being displayed. Integer parameter also works
    is_single('Irish Stew');
    // When the Post with post_title of "Irish Stew" is being displayed.
    
    is_single('beef-stew');
    // When the Post with post_name (slug) of "beef-stew" is being displayed.
    
    is_single(array(17,'beef-stew','Irish Stew'));
    // Returns true when the single post being displayed is either post ID 17,
    // or the post_name is "beef-stew", or the post_title is "Irish Stew".
    // Note: the array ability was added in version 2.5.
    
  • 示例3

    这是为了演示不同类型的单页

    if ( is_single() ) {
        // Run code only for Single post-type 'post'
    }
    
    if ( is_page() ) {
        // Run code only for Single post-type 'page'
    }
    
    
    if ( is_singular() ) {
        // Run code for any Singular post of any post type   
    }