Best way to add post data in a block

Support MB Blocks Best way to add post data in a blockResolved

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #37117
    Thomas NelsonThomas Nelson
    Participant

    Hi, 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?

    #37129
    Long NguyenLong Nguyen
    Moderator

    Hi 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>

    #37138
    Thomas NelsonThomas Nelson
    Participant

    Hi 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!

    #37160
    Long NguyenLong Nguyen
    Moderator

    Hi 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 ) }}

    #37171
    Thomas NelsonThomas Nelson
    Participant

    Thanks 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.

    #37185
    Long NguyenLong Nguyen
    Moderator

    Hi 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_callback

    #37190
    Thomas NelsonThomas Nelson
    Participant

    Got it, thanks Long!

Viewing 7 posts - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.