MB View - Gridbuilder Card
- This topic has 12 replies, 4 voices, and was last updated 2 years, 4 months ago by
Peter.
-
AuthorPosts
-
July 1, 2022 at 10:38 PM #36770
David Whritenour
ParticipantHello,
I am attempting to display a MB View within a Gridbuilder Card.
However, no data is displayed.-I am using the MB View's generated shortcode within a Gridbuilder Raw Content Block.
-The MB View displays fine in other locations outside of Gridbuilder Content, i.e. on a page.
-MB Custom Fields display fine within the Gridbuilder Card using the Gridbuilder Custom Field Block.Any suggestions would be greatly appreciated.
Thanks,
DavidJuly 3, 2022 at 10:41 AM #36778Long Nguyen
ModeratorHi David,
You can try to set the view Type to Shortcode then add the shortcode to the Gridbuilder card to display view content. Screenshot https://monosnap.com/file/UZPOaDZOYrAeCoIYPXT2lSX5sRG6rh
November 29, 2022 at 7:25 PM #39388AnLip
ParticipantHi. I'm having the same issue. I want to display on a WPGB card three MB custom fields with text in between :
{{cf_type}} of {{cf_surface}} square meters, in {{cf_city}}
and should display :
House of 200 square meters, in Berlini created a MB View that works fine on a Single Template, but does not work on a WPGB card. WPGB support only refers to creating a "custom block" but won't help further. They have documentation here https://docs.wpgridbuilder.com/resources/filter-blocks/ and I tried adapting one of their code examples ("email block") to this :
<?php function register_custom_field_email_block( $blocks ) { // "custom_field_email_block" corresponds to the block slug. $blocks['custom_field_email_block'] = [ 'name' => 'MG - Infos Projet', 'render_callback' => 'render_custom_field_email_block', ]; return $blocks; } add_filter( 'wp_grid_builder/blocks', 'register_custom_field_email_block' ); function render_custom_field_email_block() { // Object can be a post, term or user. $object = wpgb_get_object(); // If this is not a post (you may change this condition for user or term). if ( ! isset( $object->post_type ) ) { return; } // You have to change "custom_field_name" by yours. echo do_shortcode('[mbv name="wpgb-card-meta"]'); } ?>
But it doesn't work, however I use the shortcode in WPGB card (whether inserting raw HTML and using the shortcode, or whether using the created custom block. Either way, the intermediate text displays ("... of ... square meters, in ...") but not the custom field values.
November 29, 2022 at 10:54 PM #39400AnLip
ParticipantI got an answer from GridBuilder support that I'd need to add the post ID to the shortcode.
They gave me an example for ACF : [acf field="field_name" post_id= "{{ post.id }}"]So I suppose it would be for MB : [mbv name="wpgb-card-meta" post_id= "{{ post.id }}"]
But it's still not working.
November 30, 2022 at 10:58 PM #39422Peter
ModeratorHello,
The view template support passing the variable to the view shortcode. So you can pass the post ID and use the ID in the template as well. Can you please share the view code that you use variable
post_id
?Please read more on the documentation https://docs.metabox.io/extensions/mb-views/#custom-data
November 30, 2022 at 11:02 PM #39423AnLip
ParticipantHi.
Not sure what you mean, but if you could give me a code to test i will definitely do it.This is the MB View code I use in the Template part :
<div> {% for item in post.mg_projet_type %} {{ item.name }} {% endfor %} de {{ post.mg_projet_surface }}m2, à {{ post.mg_projet_localisation }}</div>
This is the shortcode the way I use it (once in a WPGB custom block with code written above, and once in a native raw content block (HTML) from WPGB :
[mbv name="wpgb-card-meta" post_id="{{ post.id }}"]
December 1, 2022 at 9:48 PM #39436Peter
ModeratorHello,
To understand how to pass the custom data to the View shortcode, please do the following steps:
1. View code: just print out the post ID
Post ID: {{ post_id }}
2. PHP code that works with Gridbuilder
function render_custom_field_email_block() { // Object can be a post, term or user. $object = wpgb_get_object(); // If this is not a post (you may change this condition for user or term). if ( ! isset( $object->post_type ) ) { return; } // You have to change "custom_field_name" by yours. echo do_shortcode('[mbv name="wpgb-card-meta" post_id='. $object->ID .']'); //pass the ID from object above }
Let me know how it goes.
December 1, 2022 at 10:01 PM #39438AnLip
ParticipantHi. Thanks for your answer.
I am not sure what you mean by : "1. View code: just print out the post ID"
Post ID: {{ post_id }}
? What do I do with this piece of code ? Do I replace all my View code and leave only this ? If so, it prints "Post ID : 1234", but I do not want to see it on the frontend. And it does not make the data from the custom fields appear on the card, I still only get the inter-text.
December 2, 2022 at 9:50 PM #39459Peter
ModeratorHello,
If the view template output the post ID of the card correctly, you can create the code to get any data of the post with the ID. For example:
View code:
{{ mb.rwmb_meta( 'custom_field_id', '', post_id ) }}
Please read more on the documentation https://docs.metabox.io/functions/rwmb-meta/
https://docs.metabox.io/extensions/mb-views/#running-php-functionsDecember 2, 2022 at 11:31 PM #39463AnLip
ParticipantHi Peter, thank you for looking into this.
Sorry but doing it this way breaks the site display. If I place the code you gave me in the View (and replace the "custom_field_id" by my custom field ID), it messes up the grid on the frontend, something is wrong with the code, or I don't have enough information as to what has to go where exactly.
I will try to be precise in what I have now :
- View Template ={{ mb.rwmb_meta( 'mg_projet_localisation', '', post_id }}
nothing else than that is written in the View Template. Should it be wrapped into something such as a <div> ?. The shortcode for this view is[mbv name="wpgb-card-meta"]
- Custom code loaded with snippet plugin (WpCodeBox) that creates a custom block using the View shortcode :
<?php function register_custom_field_email_block($blocks) { // "custom_field_email_block" corresponds to the block slug. $blocks["custom_field_email_block"] = [ "name" => "MG - Infos Projet", "render_callback" => "render_custom_field_email_block", ]; return $blocks; } add_filter("wp_grid_builder/blocks", "register_custom_field_email_block"); function render_custom_field_email_block() { // Object can be a post, term or user. $object = wpgb_get_object(); // If this is not a post (you may change this condition for user or term). if ( ! isset( $object->post_type ) ) { return; } // You have to change "custom_field_name" by yours. echo do_shortcode('[mbv name="wpgb-card-meta" post_id='. $object->ID .']'); //pass the ID from object above }
- Second option to show the content of the View template : in WP GridBuilder I use a normal content block and set it to "custom content" and paste the shortcode in the raw HTML field :
[mbv name="wpgb-card-meta"]
Both options give me the same result (according to the steps since I opened this thread) : either nothing (only the simple text, not the custom fields), either only the post ID, either it breaks the layout. It never has outputted any data from the custom fields.
December 3, 2022 at 11:04 AM #39473Peter
ModeratorHello,
1. I'm trying to explain the Meta Box feature and let you know how it works and what it would display on the frontend. If you want to style your data with some custom div and CSS code, just do it on your own.
2. Can you please share some screenshots (full screen) of
- Custom fields
- Card (post)
- Custom fields value add to the cardDecember 6, 2022 at 8:33 PM #39554AnLip
ParticipantHi,
Please find links to screen captures.
At the moment I am using the MB View shortcode in a block in WP GridBuilder and get the same results whether I use :
- [mbv name="wpgb-card-meta" post_id="{{ post.id }}"]
- [mbv name="wpgb-card-meta"]
It shows the same values on every post (instead of each card having it's own values).https://pasteboard.co/6QhYX3PLrms8.png
https://pasteboard.co/AuH393fu1DEk.png
https://pasteboard.co/omqNIskAbXVC.pngHope you can help me understand how to fetch the values of each post.
December 7, 2022 at 10:55 PM #39592Peter
ModeratorHello,
I do not see you use the custom variable in the view content. Can you please read my replies above again?
https://support.metabox.io/topic/mb-view-gridbuilder-card/#post-39436
https://support.metabox.io/topic/mb-view-gridbuilder-card/#post-39459View content:
{{ mb.rwmb_meta( 'mg_projet_localisation', '', post_id ) }}
(missing the close parenthesis)If it still does not work, I recommend using PHP code instead of Twig code (View) to display the post meta in the card. For example:
function render_custom_field_email_block() { // Object can be a post, term or user. $object = wpgb_get_object(); // If this is not a post (you may change this condition for user or term). if ( ! isset( $object->post_type ) ) { return; } // You have to change "custom_field_name" by yours. echo rwmb_meta( 'mg_projet_localisation', '', $object->ID ) ; //pass the ID from object above }
-
AuthorPosts
- You must be logged in to reply to this topic.