函数文档

get_page()

💡 云策文档标注

概述

get_page() 函数用于根据页面 ID 或页面对象检索页面数据,但自 WordPress 3.5.0 起已弃用,建议使用 get_post() 替代。

关键要点

  • 函数已弃用:从 WordPress 3.5.0 版本开始,get_page() 被标记为弃用,开发者应改用 get_post() 函数。
  • 参数说明:接受页面 ID 或 WP_Post 对象作为必需参数,可选参数包括输出类型(OBJECT、ARRAY_A、ARRAY_N)和过滤器('raw'、'edit'、'db'、'display')。
  • 返回值:成功时返回 WP_Post 对象或数组,失败时返回 null。
  • 内部实现:函数内部直接调用 get_post(),传递相同参数,确保兼容性。

代码示例

$id = 0; // 替换为页面 ID
$p = get_page($id);
$t = $p->post_title;
echo '<h3>'.apply_filters('post_title', $t).'</h3>'; // 标题用 h3 包裹
echo apply_filters('the_content', $p->post_content);

注意事项

  • 弃用警告:在开发新代码时,避免使用 get_page(),优先使用 get_post() 以提高代码的长期维护性。
  • 参数传递:注意 $page 参数通过引用传递,但实际使用中通常无需特别处理。

📄 原文内容

Retrieves page data given a page ID or page object.

Description

Use get_post() instead of get_page() .

Parameters

$pageint|WP_Postrequired
Page object or page ID. Passed by reference.
$outputstringoptional
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

$filterstringoptional
How the return value should be filtered. Accepts 'raw', 'edit', 'db', 'display'. Default 'raw'.

Return

WP_Post|array|null WP_Post or array on success, null on failure.

Source

function get_page( $page, $output = OBJECT, $filter = 'raw' ) {
	return get_post( $page, $output, $filter );
}

Changelog

Version Description
3.5.0 Deprecated. Use get_post()
1.5.1 Introduced.

User Contributed Notes

  1. Skip to note 2 content

    Add the content of a page anywhere with separated title.

    $id = 0; // add the ID of the page where the zero is
    $p = get_page($id);
    $t = $p->post_title;
    echo '<h3>'.apply_filters('post_title', $t).'</h3>'; // the title is here wrapped with h3
    echo apply_filters('the_content', $p->post_content);