> WordPress中文手册 > wordpress按文章编号检索某篇文章函数: wp_get_single_post()

【说明】
按文章编号检索某篇文章。

【用法】

<?PHP wp_get_single_post( $postid, $mode ) ?>

【参数】

$postid

(整数)(可选)文章编号

默认值: 0

$mode

(字符)(可选)如何返回结果。结果应为常量:OBJECT, ARRAY_N, or ARRAY_A

默认值:OBJECT

【返回的值(对象 | 数组)】

文章对象或数组,该对象或数组所包含的内容和信息应含有两个附加字段(或关键字): ‘post_category’ 和 ‘tags_input’。

【示例】

用法:get_post()
用法:wp_get_post_categories()
用法:wp_get_post_tags()
【修改记录】

自1.1.0版本后

【源文件】

wp_get_single_post() is located in wp-includes/post.php.

/**
 * Retrieve a single post, based on post ID.
 *
 * Has categories in 'post_category' property or key. Has tags in 'tags_input'
 * property or key.
 *
 * @since 1.0.0
 *
 * @param int $postid Post ID.
 * @param string $mode How to return result, either OBJECT, ARRAY_N, or ARRAY_A.
 * @return object|array Post object or array holding post contents and information
 */
function wp_get_single_post($postid = 0, $mode = OBJECT) {
 $postid = (int) $postid;

 $post = get_post($postid, $mode);

 if (
  ( OBJECT == $mode && empty( $post->ID ) ) ||
  ( OBJECT != $mode && empty( $post['ID'] ) )
 )
  return ( OBJECT == $mode ? null : array() );

 // Set categories and tags
 if ( $mode == OBJECT ) {
  $post->post_category = array();
  if ( is_object_in_taxonomy($post->post_type, 'category') )
   $post->post_category = wp_get_post_categories($postid);
  $post->tags_input = array();
  if ( is_object_in_taxonomy($post->post_type, 'post_tag') )
   $post->tags_input = wp_get_post_tags($postid, array('fields' => 'names'));
 } else {
  $post['post_category'] = array();
  if ( is_object_in_taxonomy($post['post_type'], 'category') )
   $post['post_category'] = wp_get_post_categories($postid);
  $post['tags_input'] = array();
  if ( is_object_in_taxonomy($post['post_type'], 'post_tag') )
   $post['tags_input'] = wp_get_post_tags($postid, array('fields' => 'names'));
 }

 return $post;
}