WordPress获取文章数据函数:get_post

2022/07/13

WordPress函数get_post用于获取文章的数据,包括文章内容、标题、别名、摘要、发布时间等信息。

get_post( int $post = null, string $output = OBJECT, string $filter = 'raw' )

本文涉及的内容包括:

函数参数

$post

整数型,默认值:null

文章的ID,或对象

$output

字符串值,默认值:OBJECT

指定返回数据的类型,可用的值如下:

  • OBJECT:对象;
  • ARRAY_A:数组,升序排列;
  • ARRAY_N:数组,降序排列;

$filter

字符串值,默认值:raw

要应用的过滤类型

函数返回值

WP_Post Object (
	[ID] => 121
	[post_author] => 1
	[post_date] => 2017-11-28 21:44:29
	[post_date_gmt] => 2017-11-28 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] => 2018-04-08 09:04:53
	[post_modified_gmt] => 2018-04-08 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
)

注意返回的guid为文章原始链接,即动态URL,不会返回伪静态链接。

函数使用示例

<?php
	$post = get_post( 8 );
	$title = $post->post_title;
	$excerpt = $post->post_excerpt;
	$content = $post->post_content;
?>

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注