admin管理员组

文章数量:1025558

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
Add a comment  | 

2 Answers 2

Reset to default 1

Setting $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' );

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
Add a comment  | 

2 Answers 2

Reset to default 1

Setting $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