Support Forum
Hi All,
I was wondering if there was a way to render the output of a text custom field into a Gutenberg block created with MB Blocks. To add context we used the twig option to create the Gutenberg blocks and we have a custom field that we would like to use as a setting in several blocks we have created without the user having to enter it for each block placed on the page.
Thanks!
Hi Kyle,
You can use the helper function rwmb_meta() and pass the ID of the page/post as the third parameter to get text
field value of that post/page.
rwmb_meta( 'text_fieldID', '', 12345 );
If you use the Render Code area in the Builder, call a PHP function via the proxy mb
. Please read more here https://docs.metabox.io/extensions/mb-views/#running-php-functions
Thank you for your answer. So if I were to call it in code using the example like this:
{% set post = mb.get_post( 123 ) %}
{{ post.post_title }}
In my scenario my custom field has a field id of 3168
and my custom field name is product_name
Using the above code in MB blocks code I could simply call it like this (if I am understanding that correctly):
{% set post = mb.get_post( 3168 ) %}
{{ post.product_name }}
It appears blank when I test it so I am sure I am missing one small simple piece.
Thanks again for all of your help!
Please disregard my last post. I wound up getting it to work like this:
{% set post = mb.get_post() %}
{{ post.product_name }}
It renders my custom field on the front end from within a Gutenberg block but it does not show up in the editor. It is just a blank block. Any way to render here for preview?
Hi Kyle,
If you do not pass the post ID to the function get_post()
, it only works only on the frontend.
null, false, 0 and other PHP falsey values return the current global post inside the loop
Please read more on the documentation https://developer.wordpress.org/reference/functions/get_post/
The Render Code area in the Builder supports to get the current post ID by using the variable post_id
. For example:
{% set post = mb.get_post( post_id ) %}
{{ post.product_name }}
List of prebuilt variables can be used:
attribute
Block attribute. Replace attribute with anchor, align, or className.
field_id
Field value. Replace field_id with a real field ID.
is_preview
Whether in preview mode.
post_id
Current post ID.
Thank you Long!
Much appreciated!