_resolve_home_block_template()
云策文档标注
概述
_resolve_home_block_template() 函数用于返回网站首页的正确模板对象,但已在 WordPress 6.2.0 版本中被弃用。它根据站点设置(如显示首页为页面)或模板层次结构来解析模板。
关键要点
- 函数返回一个模板对象数组或 null,数组包含 postType 和 postId 字段。
- 如果站点设置中 show_on_front 为 'page' 且 page_on_front 有值,则返回页面模板。
- 否则,使用 resolve_block_template() 函数基于层次结构(front-page、home、index)解析模板。
- 此函数自 WordPress 6.2.0 起被弃用,因为站点编辑器的服务器端重定向已移除相关查询参数。
代码示例
_resolve_home_block_template() {
_deprecated_function( __FUNCTION__, '6.2.0' );
$show_on_front = get_option( 'show_on_front' );
$front_page_id = get_option( 'page_on_front' );
if ( 'page' === $show_on_front && $front_page_id ) {
return array(
'postType' => 'page',
'postId' => $front_page_id,
);
}
$hierarchy = array( 'front-page', 'home', 'index' );
$template = resolve_block_template( 'home', $hierarchy, '' );
if ( ! $template ) {
return null;
}
return array(
'postType' => 'wp_template',
'postId' => $template->id,
);
}注意事项
- 此函数已被弃用,不建议在新代码中使用,开发者应关注 WordPress 6.2.0 及更高版本的替代方案。
- 函数内部调用了 _deprecated_function() 来标记弃用状态,使用时可能会触发相关警告。
原文内容
Returns the correct template for the site’s home page.
Source
function _resolve_home_block_template() {
_deprecated_function( __FUNCTION__, '6.2.0' );
$show_on_front = get_option( 'show_on_front' );
$front_page_id = get_option( 'page_on_front' );
if ( 'page' === $show_on_front && $front_page_id ) {
return array(
'postType' => 'page',
'postId' => $front_page_id,
);
}
$hierarchy = array( 'front-page', 'home', 'index' );
$template = resolve_block_template( 'home', $hierarchy, '' );
if ( ! $template ) {
return null;
}
return array(
'postType' => 'wp_template',
'postId' => $template->id,
);
}