描述:
获取指定文章
用法:
<?php
$post = get_post( 8 );
$title = $post->post_title;
$excerpt = $post->post_excerpt;
$content = $post->post_content;
?>
参数:
$post
整数 默认值:null
文章的ID,或对象
$output
字符串 默认值:OBJECT
指定返回数据的类型,可用的值如下:
- OBJECT:对象;
- ARRAY_A:数组,升序排列;
- ARRAY_N:数组,降序排列;
$filter
字符串 默认值:raw
要应用的过滤类型
返回值:
此函数返回包括文章内容、标题、别名、摘要、发布时间等信息
WP_Post Object (
[ID] => 121
[post_author] => 1
[post_date] => 2019-12-04 09 21:44:29
[post_date_gmt] => 2019-12-04 09 13:44:29
[post_content] => 文章内容
[post_title] => 文章标题
[post_excerpt] => 文章摘要
[post_status] => publish
[comment_status] => open
[ping_status] => closed
[post_password] =>
[post_name] => 文章别名
[to_ping] =>
[pinged] =>
[post_modified] => 2019-12-04 09:04:53
[post_modified_gmt] => 2019-12-04 09 01:04:53
[post_content_filtered] =>
[post_parent] => 0
[guid] => https://www.beizigen.com/?p=121
[menu_order] => 0
[post_type] => post
[post_mime_type] =>
[comment_count] => 0
[filter] => raw
)
源文件:
/**
* Retrieves post data given a post ID or post object.
*
* See sanitize_post() for optional $filter values. Also, the parameter
* `$post`, must be given as a variable, since it is passed by reference.
*
* @since 1.5.1
*
* @global WP_Post $post
*
* @param int|WP_Post|null $post Optional. Post ID or post object. Defaults to global $post.
* @param string $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which correspond to
* a WP_Post object, an associative array, or a numeric array, respectively. Default OBJECT.
* @param string $filter Optional. Type of filter to apply. Accepts 'raw', 'edit', 'db',
* or 'display'. Default 'raw'.
* @return WP_Post|array|null Type corresponding to $output on success or null on failure.
* When $output is OBJECT, a `WP_Post` instance is returned.
*/
function get_post( $post = null, $output = OBJECT, $filter = 'raw' ) {
if ( empty( $post ) && isset( $GLOBALS['post'] ) ) {
$post = $GLOBALS['post'];
}
if ( $post instanceof WP_Post ) {
$_post = $post;
} elseif ( is_object( $post ) ) {
if ( empty( $post->filter ) ) {
$_post = sanitize_post( $post, 'raw' );
$_post = new WP_Post( $_post );
} elseif ( 'raw' == $post->filter ) {
$_post = new WP_Post( $post );
} else {
$_post = WP_Post::get_instance( $post->ID );
}
} else {
$_post = WP_Post::get_instance( $post );
}
if ( ! $_post ) {
return null;
}
$_post = $_post->filter( $filter );
if ( $output == ARRAY_A ) {
return $_post->to_array();
} elseif ( $output == ARRAY_N ) {
return array_values( $_post->to_array() );
}
return $_post;
}