Support Forum
Support › Meta Box Group › How to display clone fields subgroup
How to display clone fields using the example below?
Can give me an example of displaying clones courses within an echo
Note that my phone is clone field, and I ebixir the phone clones items on display sample field below
add_filter( 'rwmb_meta_boxes', 'prefix_register_contacts' );
function prefix_register_contacts( $meta_boxes ) {
$meta_boxes[] = array(
'title' => __( 'Contacts', 'textdomain' ),
'post_types' => 'post',
'fields' => array(
array(
'id' => 'contacts',
'type' => 'group',
'clone' => true,
'fields' => array(
array(
'id' => 'name',
'type' => 'text',
'name' => __( 'Name', 'textdomain' ),
),
array(
'id' => 'email',
'type' => 'text',
'name' => __( 'Email', 'textdomain' ),
),
array(
'id' => 'phone',
'type' => 'text',
'name' => __( 'Phone', 'textdomain' ),
'clone' => true,
),
),
),
),
);
return $meta_boxes;
}
Display
$contacts = rwmb_meta( 'contacts' );
if ( ! empty( $contacts ) ) {
foreach ( $contacts as $contact ) {
echo '<div class="contact">';
echo '<h4>', __( 'Contact info', 'textdomain' ), '</h4>';
echo '<p><label>', __( 'Name:', 'textdomain' ), '<label> ', $contact['name'], '</p>';
echo '<p><label>', __( 'Email:', 'textdomain' ), '<label> ', $contact['email'], '</p>';
echo '<p><label>', __( 'Phone number:', 'textdomain' ), '<label> ', $contact['phone'], '</p>';
echo '</div>';
}
}
The $contact['phone']
is an array, so you need to loop for it:
// Echo name, email
if ( ! empty( $contact['phone'] ) ) {
echo '<p><label>', __( 'Phone numbers:', 'textdomain' ), '<label> ';
foreach ( $contact['phone'] as $k => $phone ) {
echo $k ? ', ' : '', $phone;
}
// Or faster
echo implode( ', ', $contact['phone'] );
}
Perfect, thanks