admin管理员组

文章数量:1129019

I want to change the default template for my pages to a template called fullwidthpage.php.

I have seen this question posted and answered for pre-Gutenberg Wordpress, but I have not found a working answer for Gutenberg Wordpress (version 5.3.2 at the time of this question).

This is the non-working answer I found. When I try the non-working answer the template is set to fullwidthpage.php but when I try to update the page I get a message that says "Updating failed."

I want to change the default template for my pages to a template called fullwidthpage.php.

I have seen this question posted and answered for pre-Gutenberg Wordpress, but I have not found a working answer for Gutenberg Wordpress (version 5.3.2 at the time of this question).

This is the non-working answer I found. When I try the non-working answer the template is set to fullwidthpage.php but when I try to update the page I get a message that says "Updating failed."

Share Improve this question asked Mar 24, 2020 at 20:06 glass duoglass duo 1011 silver badge3 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 1

I was looking for the same thing. This Answer by SkyShab did the trick. Posted below for convenience.


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

As the answers by harceo and SkyShab drove me crazy I want to share how I managed to make this working. Just adding this line at any place of the page did result in an JS-error for me, as Gutenberg did not yet load its configuration. So I wait for the readystate 'complete' and add there an additional timeout as figured out by manewc. This is not recommended, but compared to all the other things I tried it works. Code is like this:

document.addEventListener("readystatechange", (evt) => { 

    /* is the whole page loaded? */
    if (document.readyState != "complete" || 
    /* is there a block editor? */
    !document.body.classList.contains( "block-editor-page" )) return;

    setTimeout(() => {
        wp.data.dispatch("core/editor").editPost( {template: "singular.php"}        );
    }, 0);
});

I created a Gist with some additional links here: https://gist.github.com/aroesler-privat/4723976e13e105e6eb9cea613f9f3bf7 - maybe this helps when you end up here ;-).

On the right hand side of your editor, at the top there are two tabs: Document and Block, click on document and go down until you see Page Attributes there is a slide down menu there where you can choose your template. See image attached:

This is an edit to the solution posted by Andreas, which I found to be very good, but had the bug that it also changed the template of a page that I opened for editing. I only want to change the template selected by default for new pages. There are a few pages that are set to other templates, and I don't want their template to change when I open them for editing. So I added a small change that makes this code not run unless the class that indicates that the page is new (versus opened for editing) is present.

// Change which template is selected by default for new pages
document.addEventListener("readystatechange", (evt) => { 

    /* is the whole page loaded? */
    if (document.readyState != "complete" || 
        /* is this a new page? */
        !document.body.classList.contains( "post-new-php" ) ||
        /* is there a block editor? */
        !document.body.classList.contains( "block-editor-page" ) 
       ) return;

    setTimeout(() => {
        wp.data.dispatch("core/editor").editPost( {template: "page-templates/interior.php"}        );
    }, 0);
});

本文标签: Change default template in the block editor (Gutenberg)