2

In a block theme I'd like to be able to make some edits to the head meta based on the (block-based) page template used. The traditional get_page_template() doesn't seem to work as hoped in this case (I'm getting a return of wp-includes/template-canvas.php). Do we currently have a method for knowing what block-based page template someone may have assigned to a post/page? (Or do I have to do something like parse the body classes, or something like that?)

2
  • I don't know that this particular approach is the way to achieve your goals here, wouldn't it be better to have multiple headers that relied on template parts for their shared pieces? There's no direct equivalent of a page template file on a technical level in a block theme, just templates that match pages. While you could try to fetch the current "template", I think this is like modifying the blueprints to a house and hiring builders to remove a door and reinstall it in the open or closed position. Composition is intended to be the solution here and no code should be necessary
    – Tom J Nowell
    Feb 9, 2022 at 17:21
  • I'm not sure you're understanding my question. I'm literally referring to the <head> element, not the <header> element. My goal is to potentially do things like add extra xmlns attributes to the html element based on expected template use.
    – jshwlkr
    Feb 9, 2022 at 19:09

1 Answer 1

0

I guess I'm a little late, but get_page_template_slug() might give you, what you are looking for. It returns a string containing the name of the page template and not its url. When working with full site editing a file might not exist but it returns the slug anyway.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.