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."
4 Answers
Reset to default 1I 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)
版权声明:本文标题:Change default template in the block editor (Gutenberg) 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736707445a1948756.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论