有一个函数正好用于此:
get_page_by_title( $page_title, $output = OBJECT, $post_type = \'page\' );
您可以这样使用它:
$page = get_page_by_title( \'Start\' );
要从特定帖子类型获取页面,请执行以下操作:
$custom = get_page_by_title( \'Start\', OBJECT, \'your_custom_post_type\' );
$post = get_page_by_title( \'Start\', OBJECT, \'post\' );
请注意,此功能将搜索所有post状态。所以你可能会收到一份草稿、一篇垃圾或私人帖子。您应使用以下选项检查结果:
$status = get_post_status( $page );
if ( \'publish\' !== $status )
return; // do not show unpublished posts
相关功能是
get_page_by_path()
:
$page = get_page_by_path( \'about/contact\' );