最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

metabox - Choosing a default page tempate (Classic => Gutenberg)

programmeradmin0浏览0评论

This code was working in the "Classic" era to set a default page template:

/* Blank Page by default */
add_action('add_meta_boxes', 'blank_default_page_template', 1);
function blank_default_page_template() {
    global $post;
    if ( 'page' == $post->post_type 
        && 0 != count( get_page_templates( $post ) ) 
        && get_option( 'page_for_posts' ) != $post->ID // Not the page for listing posts
        && '' == $post->page_template // Only when page_template is not set
    ) {
        $post->page_template = "page-template-blank.php";
    }
}

Resulting in this behaviour:

How to achieve the same in Gutenberg? Currently, it looks like:

This code was working in the "Classic" era to set a default page template:

/* Blank Page by default */
add_action('add_meta_boxes', 'blank_default_page_template', 1);
function blank_default_page_template() {
    global $post;
    if ( 'page' == $post->post_type 
        && 0 != count( get_page_templates( $post ) ) 
        && get_option( 'page_for_posts' ) != $post->ID // Not the page for listing posts
        && '' == $post->page_template // Only when page_template is not set
    ) {
        $post->page_template = "page-template-blank.php";
    }
}

Resulting in this behaviour:

How to achieve the same in Gutenberg? Currently, it looks like:

Share Improve this question asked Aug 19, 2019 at 16:19 user73539user73539
Add a comment  | 

1 Answer 1

Reset to default 0

This is possible with javascript using the wp.data api.

You can see this work in the console, by replacing the template name as needed:

wp.data.dispatch('core/editor').editPost( {template: "templates/blank.php"} )

You would just need to run that at some point after the editor sidebar components have loaded in a check to see if the page template has been set yet.

发布评论

评论列表(0)

  1. 暂无评论