Using MB Blocks in Theme Templates
- This topic has 12 replies, 5 voices, and was last updated 1 year, 1 month ago by
Vinny Green.
-
AuthorPosts
-
June 6, 2023 at 1:12 AM #42079
dmccan
ParticipantHello Meta Box Team,
I would like to use Meta Box blocks in Site Editor templates. I watched the two videos about creating MB Blocks and they both showed manually filling in a value on the right side bar. That is fine for post content, but not for a template where you want the custom field value entered on the post edit screen to be dynamically used by the template.
I have a CPT named Book Authors, which has an associated field group called Author Fields. One of the fields has the slug authors-website. All of the CPT records have a value for this URL field.
https://share.getcloudapp.com/Bluz0Q74
Using MB Builder I created a MB Block called Author Button Block. In the code area I've tried:
{{ authors-website }}
And I've tried:
{% set post = mb.get_post( post_id ) %} {{ post.authors-website }}
And I've tried this:
{% set args = { 'post_type': 'book-author', } %} {% set someposts = mb.get_posts( args ) %} {% for thispost in someposts %} {{ thispost.post_title }}: {{ thispost.authors-website }} {% endfor %}
In all cases the output value for the custom field is a zero '0', though the post title field is displayed correctly.
Can the MB Blocks be used in a template to dynamically pull the value as I want? Thank you very much.
June 6, 2023 at 9:42 PM #42089dmccan
ParticipantNow I've also tried:
{% set post = mb.get_post( post_id ) %} {{ post.author-fields.authors-website }}
June 7, 2023 at 9:11 PM #42104Peter
ModeratorHello,
Do you mean to use the custom block in Full Site Editing (Appearance > Editor)? Currently, MB Blocks does not work with FSE, our development team is working on this feature to make the custom block works in FSE.
June 8, 2023 at 12:09 AM #42112dmccan
ParticipantThank you for the information.
June 9, 2023 at 8:41 AM #42135Anh Tran
KeymasterHi David,
Can you please try changing the dash in the
authors-website
to underscore, e.g.authors_website
? Twig doesn't allow dashes in the variable name.June 9, 2023 at 8:08 PM #42141dmccan
ParticipantThank you for the suggestion. I made the change, but it just returns a zero. Note, this is a template created in the Full site Editor.
June 9, 2023 at 8:24 PM #42142dmccan
ParticipantI made a video. It is on a new site that I just created. I never used dashes. Just returns 0, unless I am missing a step or the code is wrong.
https://share.getcloudapp.com/YEu48kZr
Thanks
June 10, 2023 at 10:42 AM #42154Anh Tran
KeymasterThe problem is how to refer to the field in Twig. The correct code that I tried is:
{% set p = mb.get_post( post_id ) %} <p> <strong>Job title:</strong> {{ p.job_title }} </p>
Here is the screencast: https://monosnap.com/file/Fh9dBfRYNYj0AZ4lYG3XwlGzcFXL97
June 10, 2023 at 8:51 PM #42156dmccan
ParticipantThank you. That works as shown in the video and in the comment above. In the video you point to the field group having underscores instead of dashes. I'm guessing that would come into play if I was doing a loop on a page, so I will keep that in mind. In my test for this case the field group name didn't matter, I guess because it was not referenced.
I noticed that by default Meta Box puts dashes between the words in the field group name.
June 11, 2023 at 8:07 AM #42157Anh Tran
KeymasterDashes are not used by Twig, so we need to change them to underscores. Or you can get via the attribute method in Twig:
https://twig.symfony.com/doc/3.x/functions/attribute.html
The field group ID is the slug of the field group, and uses dashes by default. It’s rarely used in MB Blocks or MB Views, so it’s ok to keep it as it is.
June 11, 2023 at 7:36 PM #42159dmccan
Participant@Anh Tran - Thank you for your help and the resource.
January 25, 2024 at 2:08 AM #44383Dave
ParticipantDo Metabox have an ETA on full compatibility with Site Editor, it's really a deal breaker on using blocks and you've been working on it for a while now?
In site editor it's pretty easy to create blocks that will invalidate and break, that will cause console errors, that will have broken field layouts, that simply mess up in nearly every way possible.March 10, 2024 at 7:47 AM #44783Vinny Green
ParticipantAny progress on this? I wish I didn't discover this issue when trying to build with blocks.
-
AuthorPosts
- You must be logged in to reply to this topic.