How to display clone fields subgroup

Support MB Group How to display clone fields subgroup

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #3704
    Infolu OfficialInfolu Official
    Participant

    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>';
        }
    }
    #3709
    Anh TranAnh Tran
    Keymaster

    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'] );
    }
    #3804
    Infolu OfficialInfolu Official
    Participant

    Perfect, thanks

Viewing 3 posts - 1 through 3 (of 3 total)
  • The topic ‘How to display clone fields subgroup’ is closed to new replies.