admin管理员组文章数量:1434972
I've chosen a custom page(-post) to be my front page. Now I want to use a block template only on that page, when editing it in the Gutenberg editor. As I understand it I have to add it on "init" or close to it, before I know the post_ID so I can't do a if ( get_option( 'page_on_front' ) === $post_ID )
.
What are my options?
Edit: I've tried this but since is_front_page() is returning 'false' it doesn't work:
function home_block_template() {
$post_type_object = get_post_type_object( 'post' );
if ( is_front_page() ) {
$post_type_object->template = array(
array( 'core/image', array() ),
);
}
}
add_action( 'init', 'home_block_template' );
I've chosen a custom page(-post) to be my front page. Now I want to use a block template only on that page, when editing it in the Gutenberg editor. As I understand it I have to add it on "init" or close to it, before I know the post_ID so I can't do a if ( get_option( 'page_on_front' ) === $post_ID )
.
What are my options?
Edit: I've tried this but since is_front_page() is returning 'false' it doesn't work:
function home_block_template() {
$post_type_object = get_post_type_object( 'post' );
if ( is_front_page() ) {
$post_type_object->template = array(
array( 'core/image', array() ),
);
}
}
add_action( 'init', 'home_block_template' );
Share
Improve this question
edited Apr 3, 2019 at 15:03
Richard B
asked Apr 2, 2019 at 20:13
Richard BRichard B
3981 gold badge5 silver badges19 bronze badges
2 Answers
Reset to default 1Setting $post_type_object->template
seems to be done on 'init' (or close to it) while is_front_page()
is set later, so I had to use $_GET['post']
instead. I also changed get_post_type_object( 'post' )
to 'page'. Like this:
add_action( 'init', 'home_block_template' );
function home_block_template() {
if ( ! is_admin() || ! isset( $_GET['post'] ) || get_option( 'page_on_front' ) !== $_GET['post'] ) {
return false;
}
$post_type_object = get_post_type_object( 'page' );
$post_type_object->template = array(
array( 'core/list' ),
);
}
The following code should work for you let me know if you face any problem.
function myplugin_register_template() {
$post_type_object = get_post_type_object( 'post' );
if( is_front_page() ) {
$post_type_object->template = array(
array( 'core/image' ), // add your core/custom blocks here
);
}
}
add_action( 'init', 'myplugin_register_template' );
本文标签: frontpageHow do I activate a certain block template only when editing the front page
版权声明:本文标题:frontpage - How do I activate a certain block template only when editing the front page? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745627895a2667086.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论