Rendering components only in the _document.twig template #367
-
Hi guys, Great project. I wonder if you have some toughts on rendering components in different places. I am trying to see how to tackle the following question: For example, a CustomComponent is a image component that you can add to a wordpress page. You want this image to be different on every page and you want to place it inside the header instead of the main content. How do you render a component only in the header instead of the main content? As far as I understand, in the starter theme we have _document.twig main template and then the page.twig which renders the components per page. In order to render a specific component (CustomComponent) in the header I can do the following inside the _document.twig template:
Works great, the image is being rendered in the header, however if I do that, the CustomComponent is being rendered twice, one in the _document.twig (header) template and also in page.twig (main content) because page.twig template renders all compontents that are available on this specific page:
Is there any way to exclude this specific component in the page.twig template? so the CustomCompontent will be rendered only once in the header? how would you tackle this? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
Hi @Auroraboreali-s, I solve such as requirements by creating a static component field group at the required pages inside the admin area. Inside the component Example:
Hope this helps? |
Beta Was this translation helpful? Give feedback.
-
@timohubois Thank you, I wasn't sure how to work this out inside the functions.php of the component, however it brought me an idea to make a headerComponents.php (similar setup to pageComponents.php inside the fieldGroups folder) which enables a new header component section in the admin for all pages and where the CustomCompontent layout is stored. This is then being rendered via _document.twig like so:
it seems to be working. I'm not sure if that is correct interpretation of what you described though. |
Beta Was this translation helpful? Give feedback.
Hi @Auroraboreali-s, I solve such as requirements by creating a static component field group at the required pages inside the admin area. Inside the component
functions.php
you can use the ACFComposer to create a new fieldGroup.Example: