WordPress根据页面标题获取页面对象函数:get_page_by_title

2022/07/13

WordPress函数get_page_by_title根据页面标题获取页面对象,如果有相同名称标题的页面,则返回最早发布的页面。

get_page_by_title( string $page_title, string $output = OBJECT, string|array $post_type = 'page' )

本文涉及的内容包括:

函数参数

$page_title

字符串

页面标题

$output

字符串

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

  • OBJECT:返回对象;
  • ARRAY_A:返回关联数组;
  • ARRAY_N:返回数值数组;

$post_type

字符串,默认值:page

文章类型

函数返回值

WP_Post Object
(
    [ID] => 2
    [post_author] => 1
    [post_date] => 2020-11-27 11:37:03
    [post_date_gmt] => 2020-11-27 03:37:03
    [post_content] =>
    [post_title] => 关于我们
    [post_excerpt] => 
    [post_status] => publish
    [comment_status] => closed
    [ping_status] => closed
    [post_password] => 
    [post_name] => about
    [to_ping] => 
    [pinged] => 
    [post_modified] => 2020-11-27 14:45:21
    [post_modified_gmt] => 2020-11-27 06:45:21
    [post_content_filtered] => 
    [post_parent] => 0
    [guid] => https://www.beizigen.com/?page_id=2
    [menu_order] => 0
    [post_type] => page
    [post_mime_type] => 
    [comment_count] => 0
    [filter] => raw
)

函数使用示例

$pages = get_page_by_title('关于我们');
echo '<a href="' . get_page_link($pages->ID) . '">' . $pages->post_title . '</a>';

发表回复

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