MB Group Clone Shortcode
- This topic has 11 replies, 2 voices, and was last updated 5 years, 6 months ago by
Anh Tran.
-
AuthorPosts
-
September 20, 2019 at 11:15 AM #16206
Brian
ParticipantHi 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!
September 23, 2019 at 4:18 PM #16238Anh Tran
KeymasterHi 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"]
September 24, 2019 at 11:46 AM #16259Brian
ParticipantHi 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!
September 24, 2019 at 12:00 PM #16262Anh Tran
KeymasterHi 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
September 25, 2019 at 10:45 AM #16282Brian
ParticipantHi 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!
September 27, 2019 at 10:26 AM #16318Anh Tran
KeymasterHi 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:
September 27, 2019 at 11:58 AM #16323Brian
ParticipantHi 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!
September 30, 2019 at 2:44 PM #16344Anh Tran
KeymasterHi 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>
September 30, 2019 at 10:13 PM #16351Brian
ParticipantHi 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!October 2, 2019 at 11:47 AM #16364Anh Tran
KeymasterHi 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
October 6, 2019 at 12:35 AM #16403Brian
ParticipantHi 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!
October 7, 2019 at 8:51 AM #16409Anh Tran
KeymasterNothing special, if you want to select a post/page, then
post
field is a little better. Otherwise, a normalselect_advanced
is better. -
AuthorPosts
- You must be logged in to reply to this topic.