admin管理员组文章数量:1122846
I am trying to use a custom template for posts of a given category in a block theme. I have created the template file in html (post-show.html). And I have edited the template in the site editor. As I would have done before block themes, I then included this in my functions.php file:
// add template by category
function get_custom_template( $single_template ) {
global $post;
if ( in_category('show') ) {
$single_template = dirname( __FILE__ ) . '/templates/post-show.html';
}
return $single_template;
}
add_filter( "single_template", "get_custom_template" );
When I then go to load a post of category "show", WP serves the actual post-show.html file, not the template that has been edited in the site editor.
Any ideas how I should be going about doing this in a block theme instead?
I am trying to use a custom template for posts of a given category in a block theme. I have created the template file in html (post-show.html). And I have edited the template in the site editor. As I would have done before block themes, I then included this in my functions.php file:
// add template by category
function get_custom_template( $single_template ) {
global $post;
if ( in_category('show') ) {
$single_template = dirname( __FILE__ ) . '/templates/post-show.html';
}
return $single_template;
}
add_filter( "single_template", "get_custom_template" );
When I then go to load a post of category "show", WP serves the actual post-show.html file, not the template that has been edited in the site editor.
Any ideas how I should be going about doing this in a block theme instead?
Share Improve this question asked Nov 22, 2022 at 14:06 kosherkosher 2602 silver badges11 bronze badges 3 |1 Answer
Reset to default 0From reading this article I decided to create a post-show.php file and point to that in my get_custom_template function. Then, I took the meat of the post-show template - essentially everything but the header and footer - that I had created in the full site editor and created a template part called "show" out of it. Then, in my post-show.php file, I added all the other parts of the page, called the header and footer using block_header_area() and block_footer_area() and finally added the "show" template part using block_template_part().
It all works. But this largely defeats the purpose of full site editing in that I will have to go in and change this php template file should I want to add other parts to my posts outside the "show" template part (and it would be even worse if I was allowing other users to edit template files in the full site editor). What I think that we would need is some sort of block_template_part function for entire templates. And I am still hoping there is some other solution that I am not yet privy to.
本文标签: Template for posts of category in block theme
版权声明:本文标题:Template for posts of category in block theme? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736292311a1928907.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
templates/post-show.html
. How you would do this in a block theme I'm not sure, but that at least explains why you're getting this behaviour, and thatsingle_template
isn't an option for an answer – Tom J Nowell ♦ Commented Nov 22, 2022 at 14:56