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
  • 3 I don't believe that particular filter is used in block themes, so your HTML file is being loaded the same way a classic theme template would be loaded, and this filter tells WP explicitly that a classic template file is present and should be used, and its name is 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 that single_template isn't an option for an answer – Tom J Nowell Commented Nov 22, 2022 at 14:56
  • I appreciate the comment, Tom. And that is exactly what I was thinking. I just wish I knew how to accomplish it in a block theme! – kosher Commented Nov 22, 2022 at 19:18
  • It seems that the discussion here - developer.wordpress.org/block-editor/explanations/architecture/… - relates to this issue. But it doesn't offer a solution. – kosher Commented Nov 22, 2022 at 21:05
Add a comment  | 

1 Answer 1

Reset to default 0

From 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