WordPress根据页面路径获取页面对象函数:get_page_by_path

背字根

WordPress函数get_page_by_path根据页面路径获取页面对象

get_page_by_path( string $page_path, string $output = OBJECT, string|array $post_type = 'page' )

函数参数

$page_path

字符串

页面的路径,不包含域名部份,例如:

/about/contact

$output

字符串

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

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

$post_type

字符串,默认值:page

文章类型

函数返回值

WP_Post Object
(
    [ID] => 15
    [post_author] => 1
    [post_date] => 2020-11-27 16:37:42
    [post_date_gmt] => 2020-11-27 08:37:42
    [post_content] => 
    [post_title] => 联系我们
    [post_excerpt] => 
    [post_status] => publish
    [comment_status] => open
    [ping_status] => closed
    [post_password] => 
    [post_name] => contact
    [to_ping] => 
    [pinged] => 
    [post_modified] => 2021-02-22 11:00:05
    [post_modified_gmt] => 2021-02-22 03:00:05
    [post_content_filtered] => 
    [post_parent] => 2
    [guid] => https://www.beizigen.com/?page_id=15
    [menu_order] => 0
    [post_type] => page
    [post_mime_type] => 
    [comment_count] => 0
    [filter] => raw
)

函数使用示例

$pages = get_page_by_path('/about/contact');
echo '<a href="' . get_page_link($pages->ID) . '">' . $pages->post_title . '</a>';

扩展阅读

get_page_by_path()函数位于:wp-includes/post.php

相关函数:

个赞
扫码关注背字根

一个热爱Web开发的大男孩