Custom Fields
Re-Envisioned
Support Meta Box Group Nested group php call

  • Creator
    Topic
  • #4354
    sergeysemenov
    Participant

    Hi!
    I can’t figure out how to display my nested groups in the frontend.
    I’m average in php.
    Could you provide me the final php code to display this in the frontend with my params?
    I hope your answer would be useful to all ‘average php’ users like me.
    Thanks in advance…

    Here is my functions.php

    // Group
            array(
                
                'id'   => "{$prefix}sector",
                'type' => 'group',
                'clone'  => true,
    	    'sort_clone' => false,
                'fields' => array(
    	            
    	            array(
    			'name' => __( 'Name1', 'rw_' ),
    			'id'   => "{$prefix}sector-heading",
    			'type' => 'heading',
    				
    			),
    	            
    	            array(
    			'name' => __( 'Name2', 'rw_' ),
    			'id'   => "{$prefix}sector-title",
    			'type' => 'textarea',
    			'cols' => 10,
    			'rows' => 1,
    				
    			),
    
                                   // Group nested
    				
    				array(
                
    				 'id'  => "{$prefix}sector-object",
    				'type' => 'group',
    				'clone'  => true,
    				'sort_clone' => true,
    				'fields' => array(
    				
    	            
    						array(
    						'name'  => __( 'Name3', 'rw_' ),
    						'id'    => "{$prefix}sector-object-img",
    						'type'  => 'image_advanced',
    						'max_file_uploads' => 1,
    						),
    	            
    						array(
    						'name' => __( 'Name4', 'rw_' ),
    						'id'   => "{$prefix}sector-object-description",
    						'type' => 'textarea',
    						'cols' => 10,
    						'rows' => 4,
    						),
    			
    			                   ),
    				       ),
    	            
                                        ),
                                      ),
    	                        )
                                 );
Viewing 5 replies - 1 through 5 (of 5 total)
  • Author
    Replies
  • #4364
    Anh Tran
    Keymaster

    No problem, here you are:

    <?php
    $prefix = '';
    $sectors = rwmb_meta( "{$prefix}sector" );
    if ( ! empty( $sectors ) ) {
    	foreach ( $sectors as $sector ) {
    		$sector_heading = isset( $sector["{$prefix}sector-heading"] ) ? $sector["{$prefix}sector-heading"] : '';
    		echo $sector_heading;
    
    		$sector_title = isset( $sector["{$prefix}sector-title"] ) ? $sector["{$prefix}sector-title"] : '';
    		echo $sector_title;
    
    		$objects = isset( $sector["{$prefix}sector-object"] ) ? $sector["{$prefix}sector-object"] : array();
    		foreach ( $objects as $object ) {
    			$object_img = isset( $object["{$prefix}sector-object-img"] ) ? $object["{$prefix}sector-object-img"] : '';
    			if ( $object_img ) {
    				echo '<img src="' . wp_get_attachment_image_url( $object_img, 'size' ) . '">';
    			}
    			$object_desc = isset( $object["{$prefix}sector-object-description"] ) ? $object["{$prefix}sector-object-description"] : '';
    			echo $object_desc;
    		}
    	}
    }
    #4367
    sergeysemenov
    Participant

    Thanks for assistance, Anh Tran.
    One more question.
    What if my field is in nested group, has 10 images uploaded and is used as a slider in the frontend? How do I call these images?

    array(
    						'name'  => __( 'Name3', 'rw_' ),
    						'id'    => "{$prefix}sector-object-img",
    						'type'  => 'image_advanced',
    						'max_file_uploads' => 10,
    						),
    #4370
    sergeysemenov
    Participant

    I suppose there’s something wrong with image_advanced field in nested groups.
    I tried to exclude nested group, so for now my metabox looks like:

    // Group
    			array(
                'name' => 'Сектор инфраструктуры',
                'id'               => "{$prefix}sector",
                'type' => 'group',
                'clone'  => true,
    			'sort_clone' => false,
                'fields' => array(
    	            
    	            
    	            
    	            array(
    				'name' => __( 'Название сектора', 'rw_' ),
    				
    				'id'   => "{$prefix}sector-title",
    				'type' => 'textarea',
    				'cols' => 10,
    				'rows' => 1,
    				
    				),
    				
    				array(
    						'name' => __( 'Описание сектора', 'rw_' ),
    						'id'   => "{$prefix}sector-description",
    						'type' => 'textarea',
    						'cols' => 10,
    						'rows' => 4,
    						),
    				
    					
    				
    	            array(
    						'name'             => __( 'Изображение объекта', 'rw_' ),
    						'id'               => "{$prefix}sector-object-image",
    						'type'             => 'image_advanced',
    						'max_file_uploads' => 10,
    						),
    	            
    				
    	            
                ),
            ),

    And I call it in frontend

    <?php $sectors = rwmb_meta( 'rw_sector' );
    if ( ! empty( $sectors ) ) {
    foreach ( $sectors as $sector )
    {
        echo '<div class="object-block">
        
        		<div class="object">
        		
        			<div class="sector-object-text">';
        		
    				$title = isset( $sector['rw_sector-title'] ) ? $sector['rw_sector-title'] : '';
    		
    				echo '<h3>' . $title . '</h3>';
        			
    				$description = isset( $sector['rw_sector-description'] ) ? $sector['rw_sector-description'] : '';
    		
    				echo '<p>' .$description.'</p>';
    				
    					
    				echo 
    				'</div>';
        		
        		
    				$object_img = isset( $sector['rw_sector-object-image'] ) ? $sector['rw_sector-object-image'] : '';
    				if ( $object_img ) {
    				
    				echo 
    				
    				'<div class="sector-object-img">
    						
    					<div class="slider slider-for">
        		
    							<img src="' . wp_get_attachment_image_url( $object_img, 'type=image_advanced&size=full&multiple=true' ) . '">
        		
    					</div>
    				</div>';
    				}
        		
        		
        		
        echo	
        		
        		'</div>
    		
    		</div>';
    	} 
    } ?>

    Title and description are displayed, but no images.
    I even tried with 'max_file_uploads' => 1, but no luck

    #4378
    Anh Tran
    Keymaster

    Ah, sorry, for image fields, the returned value is always an array. You need to change the code to:

    $object_imgs = isset( $sector['rw_sector-object-image'] ) ? $sector['rw_sector-object-image'] : array();
    if ( ! empty( $object_imgs ) ) {
        foreach ( $object_imgs as $object_img ) {
            echo '<div class="sector-object-img">
                <div class="slider slider-for">
                <img src="' . wp_get_attachment_image_url( $object_img, 'type=image_advanced&size=full&multiple=true' ) . '">
            </div>
            </div>';
        }
    }
    #4383
    sergeysemenov
    Participant

    Thanks, Anh Tran! It’s all done! Topic closed

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Nested group php call’ is closed to new replies.

Newsletter

Subscribe to our newsletter to receive news and tutorials for Meta Box and WordPress.


grid gear info copy caret-up caret-down trash