Best way to add post data in a block
- This topic has 6 replies, 2 voices, and was last updated 2 years, 9 months ago by
Thomas Nelson.
-
AuthorPosts
-
July 22, 2022 at 4:32 AM #37117
Thomas Nelson
ParticipantHi, I have a MB Block created that utilizes 2 CPT selects (advanced). I would like to show more data from the post and can't quite figure out the best way to do that. I am using the code option for block render settings:
<div class="officer"> <h3>{{ office }}</h3> <h4>{{ member }}</h4> </div>
Can I use the code editor to get the post data?
July 22, 2022 at 4:26 PM #37129Long Nguyen
ModeratorHi Thomas,
If the field is registered in the block, please use the helper function
mb_get_block_field()
to get the field value. Read more on the documentation https://docs.metabox.io/extensions/mb-blocks/#render_callback<h3>{{ mb.mb_get_block_field( 'office' ) }}</h3>
If the field is registered as the post meta, please use the helper function
rwmb_meta()
to get the field value. Read more on the documentation https://docs.metabox.io/functions/rwmb-meta/<h3>{{ mb.rwmb_meta( 'office', '', post_id ) }}</h3>
July 23, 2022 at 4:04 AM #37138Thomas Nelson
ParticipantHi Long,
Thanks for the info. I'm still a bit confused though.
The block has 2 fields member and office that are select advanced, pulling data from the member and office CPT's.<div class="officer"> <h3>{{ office }}</h3> <p>{{ mb.rwmb_meta( 'date_begins ', '', $post_id ) }}</p> //more office data <h4>{{ member }}</h4> //more member data </div>
So first question, do I need to use a php template file or can it be done in the "code" editor of the block custom field group?
Either way, how would I get the post_id from the selected CPT records to get the data from the selected post?
Thanks for any help!July 24, 2022 at 9:56 PM #37160Long Nguyen
ModeratorHi Thomas,
1. Using the PHP template or "code" area in the builder are the same purpose to render the template for the block. The difference is you need to use Twig code in the "code" area. Please read more here https://docs.metabox.io/extensions/mb-views/#twig
2. You need to get the post ID from the selected value first, suppose that the member is a post field https://docs.metabox.io/fields/post/
{% set member_id = mb.mb_get_block_field( 'member' ) %}
then get other field values associated with that member ID
{{ mb.rwmb_meta( 'member_field', '', member_id ) }}
July 25, 2022 at 2:59 AM #37171Thomas Nelson
ParticipantThanks Long! That clears it up. And I love twig! That brings up one more question. Id the twig code going to be valid if I convert this to a function. I see the generator includes that.
July 25, 2022 at 10:09 PM #37185Long Nguyen
ModeratorHi Thomas,
If you get the PHP code of the field group and put it to the file functions.php of the theme or a plugin for implementation, you should use the PHP template (render_template setting) or PHP function (render_callback setting) to display the block.
https://docs.metabox.io/extensions/mb-blocks/#render_callbackJuly 26, 2022 at 1:51 AM #37190Thomas Nelson
ParticipantGot it, thanks Long!
-
AuthorPosts
- You must be logged in to reply to this topic.