Support Forum
Support › Meta Box Group › MB Group Clone ShortcodeResolved
Hi Anh,
I'm having a hard time understanding how to write a shortcode to output your MB Group Clone example: https://metabox.io/clone-group-custom-fields-using-meta-box-group/
$meta_boxes[] = array(
'title' => 'Car Details',
'context' => 'side',
'fields' => array(
array(
'id' => 'car',
'type' => 'group',
'clone' => true, // ALLOW βCARβ GROUP TO BE CLONED
'add_button' => '+ Add Group',
'fields' => array(
array(
'name' => 'Brand',
'id' => 'brand',
'type' => 'text',
),
array(
'name' => 'Date Release',
'id' => 'date',
'type' => 'date',
),
array(
'name' => 'Color',
'id' => 'color',
'type' => 'color',
'add_button' => 'Add Color',
),
),
),
),
);
return $meta_boxes;
}
Any chance you could show me how with the above example?
Thanks in advance!
Hi Brian,
Please try this code:
add_shortcode( 'display_cars', function( $atts ) {
$atts = wp_parse_args( $atts, [
'post_id' => get_the_ID(),
] );
$cars = rwmb_meta( 'car', '', $atts['post_id'] );
if ( empty( $cars ) ) {
return '';
}
$output = '';
foreach ( $cars as $car ) {
$output .= '<h4>', 'Car Details', '</h4>';
$output .= '<p>', 'Brand:', ' ', $car['brand'], '</p>';
$output .= '<p>', 'Date Release:', ' ', $car['date'], '</p>';
foreach ( $car['color'] as $color ) {
$output .= '<p>', 'Color:', ' <span style="color:', $color, ';"><strong>Color</strong></span></p>';
}
}
} );
Usage:
[display_cars] // for the current post
[display_cars id="123"]
Hi Anh,
That example does help and got me started. I guess I don't understand how to correctly formate what's inside the output. For my example I was hoping to just have one output as I have a dropdown tab that I wanted to include the MB data fields in.
Here is my code. https://pastebin.com/NB3ZJmUD
Am I able to do something like this with only one output?
Thanks in advance!
Hi Brian,
When the HTML code is large, it's recommended to use output buffering to echo the content. Here is the modified code: https://pastebin.com/pk6xkMLb
Hi Ahn,
Thanks for the advise on output buffering. When I copy the code it throws a syntax error. Should I remove the opening " <?php " ? That was the only thing that worked and did show my repeating drop down tabs. But my field values are not showing? Not sure why the values aren't displaying.
Here is my full metabox code: https://pastebin.com/j19tR1mZ
I'm wondering if I just labeled / missed named something. As my values are not displaying for the group fields.
Thanks in advance!
Hi Brian,
When you copy the code and paste into your theme's functions.php
file (or another file), you might not need <?php
part.
Anyway, I've modified the code. The original code has some wrong variable name: https://pastebin.com/Qtpx9SE1
Here is what it looks like in my file:
Hi Anh,
That worked and the values are showing know. Thanks for pointing that out.
I forgot to include my "select_advanced" field in the shortcode for the booking url. Since it's in a group and the whole group is clonable. How would I write the select_advanced filed to just output the url (for whatever page was selected in dropdown)? As that's what I'll used for the button url.
Again thanks!
Hi Brian,
Is the booking URL the same for all tour dates? Is that for you to select a page? If yes, then the code to outputting it is quite simple:
<?php
$page_id = rwmb_meta( 'booking_page' );
?>
<a href="<?php the_permalink( $page_id ) ?>">Book Now</a>
Hi Anh,
No the booking URL will be different for each tour date. Each date will have it's own woocommerece product page. So I was wanting to use the "select_advanced" dropdown field so that the user could select the correct woo product page for each date.
I hope that made sense?
Thanks in advance!
Hi Brian,
Then you can add the code to the meta box like this: https://pastebin.com/ernuJrQ1
And output it like this: https://pastebin.com/jzkpXYjH
Hi Anh,
That worked thank you! I changed the field to a select advanced dropdown instead of the post dropdown. Any reason for the post field vs the select advanced field?
Thanks again!
Nothing special, if you want to select a post/page, then post
field is a little better. Otherwise, a normal select_advanced
is better.