Displaying Grouped Data on Frontend

Support MB Group Displaying Grouped Data on Frontend

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #3543
    87Pancakes87Pancakes
    Participant

    I can't seem to find the documentation to display the grouped data on the frontend.

    Can you point me to it or explain how to create a loop of the fields on the frontend please.

    Thanks,

    Ben

    #3560
    Anh TranAnh Tran
    Keymaster

    Hey Ben,

    This sample code registers a group of fields: contact name, contact email, contact phone number:

    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' ),
                        ),
                    ),
                ),
            ),
        );
        return $meta_boxes;
    }

    In the single.php, you can add the following code to display contacts:

    $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>';
        }
    }
Viewing 2 posts - 1 through 2 (of 2 total)
  • The topic ‘Displaying Grouped Data on Frontend’ is closed to new replies.