Help displaying group

Support MB Group Help displaying group

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #1145
    totalmarctotalmarc
    Participant

    First of all I'm a novice so bear with me please, I need help displaying a group in a template. I set up the metabox with cloneable group which works fine:

    $meta_boxes[] = array(
    		'id'         => 'initiatives',
    		'title'      => __( 'Initiatives', 'test' ),
    		'post_types' => array( 'page' ),
    		'context'    => 'normal',
    		'priority'   => 'high',
    		'show'   => array(
    	            'relation'    => 'OR',
    	            'template'    => array( 'template-regions-home.php' ),           
    			),
    		'autosave'   => true,
    		'fields'     => array(
    		//Group
    			array(
    				'id'     => 'initiatives_group',
    				'name'   => __( 'Initiatives', 'test' ),
    				'type'   => 'group', // Group type
    				'clone'  => true,    // Can be cloned?
    				// List of child fields
    				'fields' => array(
    					
    			array(
    				'name'    => __( 'Initiative Title', 'test' ),
    				'id'      => 'initiative_title',
    				'type'    => 'text',
    			),
    			array(
    				'name' => __( 'Initiative Featured Image', 'test' ),
    				'id'   => "initiative_image",
    				'type' => 'file_input',
    			),
    					)
    );
    	return $meta_boxes;
    }

    I'm stuck how to display them in my template in rows like this, can someone please provide a working example based on above how to make this work?:

    function initiatives_box() {	
    $group_value = rwmb_meta( 'initiatives_group' ); ?>
    
    <div class="the_image"><img src="<?php echo rwmb_meta( 'initiative_image' ); ?>"></div>
    <div class="the_title"><?php echo rwmb_meta( 'initiative_title' ); ?></div>
    	}
    #1147
    Anh TranAnh Tran
    Keymaster

    Can you try this code:

    $group_value = rwmb_meta( 'initiatives_group' );
    foreach ( $group_value as $row_value )
    {
        echo '<div class="the_image"><img src="' . $row_value['initiative_image'] . '"></div>
            <div class="the_title">' . $row_value['initiative_title'] . '</div>';
    }
    #1153
    totalmarctotalmarc
    Participant

    Works great, thanks so much!

    #1154
    totalmarctotalmarc
    Participant

    Is it possible the return the value of a select_advanced field with multiple => true so it shows multiple page links selected?

    I have:

    array(
    				'name'        => __( 'Inititavie Links', 'test' ),
    				'id'          => "intiative_links",
    				'type'        => 'post',
    				'post_type'   => 'page',
    				'field_type'  => 'select_advanced',
    				'multiple'    => true,
    				'placeholder' => __( 'Select an Item', 'test' ),
    				'query_args'  => array(
    					'post_status'    => 'publish',
    					'posts_per_page' => - 1,
    				)
    			),

    Is something like this possible?:

    $group_value = rwmb_meta( 'initiatives_group' );
    foreach ( $group_value as $row_value )
    {
        echo '<div class="the_image"><img src="' . $row_value['initiative_image'] . '"></div>
            <div class="the_title">' . $row_value['initiative_title'] . '</div>
            <div class="the_links">' . $row_value['intiative_links'] . '</div>';
    }
    #1157
    Anh TranAnh Tran
    Keymaster

    Yes, it's possible. If you set multiple to true, then the value of $row_value['intiative_links'] will be an array, you need to loop through it to output the result:

    $group_value = rwmb_meta( 'initiatives_group' );
    foreach ( $group_value as $row_value )
    {
        echo '<div class="the_image"><img src="' . $row_value['initiative_image'] . '"></div>
            <div class="the_title">' . $row_value['initiative_title'] . '</div>';
        echo '<div class="the_links">';
        foreach ( $row_value['intiative_links'] as $id )
        {
             echo '<a href="' . get_permalink( $id ) . '">' . get_the_title( $id ) . '</a>';
        }
        echo '</div>';
    }
    #1160
    totalmarctotalmarc
    Participant

    Works great, much appreciated!

Viewing 6 posts - 1 through 6 (of 6 total)
  • The topic ‘Help displaying group’ is closed to new replies.