最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

frontpage - How do I activate a certain block template only when editing the front page?

matteradmin6PV0评论

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' );
Post a comment

comment list (0)

  1. No comments so far