WordPress文章函数:is_single()


【说明】

判断一个单独一面是否被显示,返回 TRUE 或者 FALSE

【用法】

  1. <?php is_single($post); ?>

【参数】
$post
(mixed) (optional) Post ID, Post Title or Post Slug

默认: None
【返回值】
(boolean)
成功 True, 失败 false.

【示例】

  1. is_single();
  2. // 当任何文章页面被显示.
  3.  
  4. is_single('17');
  5. // 当文章ID为17的被显示.
  6.  
  7. is_single(17);
  8. // 当文章ID为17的被显示。 整形的参数也可以。
  9.  
  10. is_single('Irish Stew');
  11. // 文章的标题为"Irish Stew"的被显示.
  12.  
  13. is_single('beef-stew');
  14. // 当文章的别名 post_name (slug) 是 "beef-stew" 的 被显示.
  15.  
  16. is_single(array(17,'beef-stew','Irish Stew'));
  17. // 当文章的 ID是17, 或者别名 post_name 是 "beef-stew",或者文章的标题是"Irish Stew"的返回True. 注意: 数组参数的功能是在 2.5版本添加的.

【注意】
类似功能: is_singular()

【源文件】

is_single() 在 wp-includes/query.php.

  1. /**
  2. * Is the query for a single post?
  3. *
  4. * Works for any post type, except attachments and pages
  5. *
  6. * If the $post parameter is specified, this function will additionally
  7. * check if the query is for one of the Posts specified.
  8. *
  9. * @see is_page()
  10. * @see is_singular()
  11. *
  12. * @see WP_Query::is_single()
  13. * @since 1.5.0
  14. * @uses $wp_query
  15. *
  16. * @param mixed $post Post ID, title, slug, or array of such.
  17. * @return bool
  18. */
  19. function is_single( $post = '' ) {
  20. global $wp_query;
  21.  
  22. if ( ! isset( $wp_query ) ) {
  23. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  24. return false;
  25. }
  26.  
  27. return $wp_query->is_single( $post );
  28. }
  29.  
  30.  
  31. /**
  32. * Is the query for a single post?
  33. *
  34. * Works for any post type, except attachments and pages
  35. *
  36. * If the $post parameter is specified, this function will additionally
  37. * check if the query is for one of the Posts specified.
  38. *
  39. * @see WP_Query::is_page()
  40. * @see WP_Query::is_singular()
  41. *
  42. * @since 3.1.0
  43. *
  44. * @param mixed $post Post ID, title, slug, or array of such.
  45. * @return bool
  46. */
  47. function is_single( $post = '' ) {
  48. if ( !$this->is_single )
  49. return false;
  50.  
  51. if ( empty($post) )
  52. return true;
  53.  
  54. $post_obj = $this->get_queried_object();
  55.  
  56. $post = (array) $post;
  57.  
  58. if ( in_array( $post_obj->ID, $post ) )
  59. return true;
  60. elseif ( in_array( $post_obj->post_title, $post ) )
  61. return true;
  62. elseif ( in_array( $post_obj->post_name, $post ) )
  63. return true;
  64.  
  65. return false;
  66. }