get_page_by_path()通过页面的路径获取页面信息

目录

描述

Retrieves a page given its path.

get_page_by_path() 描述

用法

<?php get_page_by_path$page_path$output$post_type ); ?>

get_page_by_path() 用法

参数

$page_path

(string) (必填) Page path.

默认值: None

$output

(string) (可选) Output type. OBJECT, ARRAY_N, or ARRAY_A.

默认值: OBJECT

$post_type

(string or array) (可选) Post type or array of post types.

默认值: 'page'

get_page_by_path() 参数

返回值

(WP_Post|null) 

WP_Post on success or null on failure.

get_page_by_path() 返回值

示例

Page Path

This is the equivalent of the 'pagename' query, as in: 'index.php?pagename=parent-page/sub-page'.

Code for the above could be written as (assuming 'parent-page/sub-page' is actually the path to a page):

For non-heirarchical custom post types, you need to use just the slug in tandem with the post_type parameter.

The functions basename() and untrailingslashit() are handy for grabbing the last part of the URL for this:

get_page_by_path() 示例

注意

  • 使用到 global: (object) $wpdb
  • Returns false for "private" pages/posts

get_page_by_path() 注意

历史

添加于 版本: 2.1.0

get_page_by_path() 历史

源文件

get_page_by_path() 函数的代码位于 wp-includes/post.php.

get_page_by_path() 源文件

相关