GeekerCode更好的WordPress主题,值得信任的WordPress主题开发商

极美的主题、极致的插件

WordPress通过页面的路径获取页面信息函数:get_page_by_path

摘要:【描述】通过页面的路径获取页面信息【使用】<?php get_page_by_path( $page_path, $output, $post_type ) ?>【参数】$page_path(string) (必

【描述】通过页面的路径获取页面信息

【使用】

<?php get_page_by_path( $page_path, $output, $post_type ) ?>

【参数】

$page_path

(string) (必须) 页面路径

Default: None

$output

(string) (可选) 输出类型. OBJECT, ARRAY_N, or ARRAY_A.

Default: OBJECT

$post_type

(string) (可选) 文章类型.

Default: page

【返回的值】(mixed)  Null when complete.

【示例】

这个等价于通过 ‘pagename’ 来查找, 类似: ‘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):

get_page_by_path('parent-page/sub-page');

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

//Returns nothing, assumes animals is the rewrite slug for the animal CPT

get_page_by_path('animals/cat', OBJECT, 'animal');

//Returns the animal with the slug ‘cat’

get_page_by_path('cat', OBJECT, 'animal');

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

$page_path = ‘animals/cat/’;

get_page_by_path( basename( untrailingslashit( $page_path ) ) , OBJECT, 'animal');

【源文件】get_page_by_path() 位于wp-includes/post.php.

 

建站!你有充足的理由选择我们

世界上超过30%的网站是由WordPress搭建,而我们是国内最靠谱的WordPress主题开发商
QQ咨询在线咨询问答互助微信号geekercode微信公众号云服务器