最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

WP - 6.7.1 Block Theme - Template Part - How to add custom class, to Footer and Header element?

programmeradmin1浏览0评论

I'm not new to Wordpress and I make my own theme, but the old way... Not as a Block Theme.

I'm trying to move on to Block Theme, And I'd like to know if there any simple possibility to add some custom class to Footer and/or Header (html tag), when using block-theme parts??

I've been searching the developer Handbook but nothing conclusive (surely missed something), I also tried to look at the code and found a filter responsible for adding default classes based on the block name block_default_classname but it does not differentiate template-part.
There is also the wp_apply_custom_classname_support function which is the closer to what I want, but I do not understand how to add the block attribute className for the <footer> or the <header> template-part.

EDIT: For maybe more explanation, here is how is made my theme and how is called the header and footer:

  • themeFolder/
    • parts/
      • header.html
      • footer.html
    • templates/
      • index.html
    • ...

index.html

<!-- wp:template-part {"slug":"header","tagName":"header"} /-->
<!-- wp:template-part {"slug":"footer","tagName":"footer"} /-->

header.html and footer.html are empty as they are "populated/created" from the editor inside the admin. All I want is to be able to add some classes along the one added by WP (wp-block-template-part) when the template part block is rendered

<footer class="wp-block-template-part">....</footer>

I'm not new to Wordpress and I make my own theme, but the old way... Not as a Block Theme.

I'm trying to move on to Block Theme, And I'd like to know if there any simple possibility to add some custom class to Footer and/or Header (html tag), when using block-theme parts??

I've been searching the developer Handbook but nothing conclusive (surely missed something), I also tried to look at the code and found a filter responsible for adding default classes based on the block name block_default_classname but it does not differentiate template-part.
There is also the wp_apply_custom_classname_support function which is the closer to what I want, but I do not understand how to add the block attribute className for the <footer> or the <header> template-part.

EDIT: For maybe more explanation, here is how is made my theme and how is called the header and footer:

  • themeFolder/
    • parts/
      • header.html
      • footer.html
    • templates/
      • index.html
    • ...

index.html

<!-- wp:template-part {"slug":"header","tagName":"header"} /-->
<!-- wp:template-part {"slug":"footer","tagName":"footer"} /-->

header.html and footer.html are empty as they are "populated/created" from the editor inside the admin. All I want is to be able to add some classes along the one added by WP (wp-block-template-part) when the template part block is rendered

<footer class="wp-block-template-part">....</footer>
Share Improve this question edited Feb 5 at 13:35 La Fabrique Info asked Feb 5 at 10:38 La Fabrique InfoLa Fabrique Info 113 bronze badges 6
  • can you add some context for what you're going to use this for? The answer to this will depend on the why/context – Tom J Nowell Commented Feb 5 at 10:57
  • The context of which to use it for isn't really relevant I reckon... For example It could be to easily force the header to be fixed at the top using Bootstrap classes. But the real question is how to add class to the element, If want to add foo or bar classes, how to do it? This is the question I'm looking answer for. – La Fabrique Info Commented Feb 5 at 11:06
  • Why is it down voted suddenly... Before with classic theme development you could add some classes to the body with simple function like body_class(), and your header or footer were completely yours to make. So Why is my question relative to context? What am I missing here? @TomJNowell – La Fabrique Info Commented Feb 5 at 13:22
  • Because different use cases have different solutions depending on the context. Be that a block variant, a synced block, the classname field in advanced panel, separate template parts, a containing block, a block style, etc etc, there is no 1 single golden path to follow here, it depends. For what it's worth I wasn't the one to downvote. I will say though that this particular approach is not particularly in keeeping with block theme development, you might find down the road that it becomes unwieldy as blocks don't contain everything that define them due to arbitrary CSS classes here and there – Tom J Nowell Commented Feb 5 at 15:56
  • e.g. in your example, it would be better to use the built in positioning tools, and to use them on the blocks themselves rather than the template part that contains them – Tom J Nowell Commented Feb 5 at 15:56
 |  Show 1 more comment

1 Answer 1

Reset to default 1

After some trial and error here is what I was looking for.

So to add classes to some Template Part Block (like header and footer), you can (cause it is surely not the only solution) add the attribute className to the markup.

<!-- wp:template-part {"slug":"footer","tagName":"footer", "className":"foo bar baz"} /-->
发布评论

评论列表(0)

  1. 暂无评论