Viewing 1 reply thread
  • Author
    Posts
    • #7653
      hozefasmile
      Member

      Hi All Piklist Experts 🙂

      I am trying to find out some easy way to display values in easy way for group and repeat fields that are multidimensional array fields.

      For example I have created a Book custom post type where there is a group repeater field editor_info which provide editor name and his image as a repeater field.

      So this is the code created for that field

      piklist('field', array(
      	'type' => 'group'
      	,'field' => 'editor_info'
      	,'label' => 'Editors Information'
      	,'list' => false
      	,'column' => 2
      	,'add_more' => true
      	,'fields' => array(
      		array(
      		'type' => 'text'
      		,'field' => 'editor_name'
      		,'label' => 'Editor Name'
      		,'columns' => 6
      		)
      		,array(
      		'type' => 'file'
      		,'field' =>'editor_image'
      		,'label' => 'Upload Image'
      		,'columns' => 6
      		,'options' => array(
      			'modal_title' => 'Add file'
      			,'button' => 'Upload'
      			)
      		)
      	)
      ));

      Ok Now to display this complex multidimensional associative array to my theme template file for book is not an easy task.

      First I got the array created for the field by this code

      $editor_info = get_post_meta($post->ID, 'editor_info', false); // to get array field
      print_r($editor_info); // to see its array information

      So on frontend display I found this array information:-

      Array ( 
      		[0] => Array ( 
      			[editor_name] => Rohit Sharma 
      			[editor_image] => Array ( 
      				[0] => 43 ) 
      			) 
      		[1] => Array ( 
      			[editor_name] => Manoj Tripathi 
      			[editor_image] => Array ( 
      				[0] => 33 ) 
      			) 
      		)

      So above information gives how multidimensional array is creating editor name and attachement post type id for images.

      But Now the real hardwork start to display this array information in proper way in the frontend.

      So I have created this loop to show editor names

      for ($i=0; $i < count($editor_info); $i++){
      			for ($j=0; $j < count($editor_info[$i]); $j++){
      				echo "<p><strong>Editor Name: </strong>" . $editor_info[$i][$j]['editor_name'] . "</p>";
      			}
      		}

      But the work is still in middle, I have to do further more complex query in this array loop to get image urls for each editor to show with his name.

      So now my question is that I have found in forum that piklist use some template file to easily loop such array fields. But I am really unaware about how to use it? There is no separate chapter on piklist documentation about template files and how to use them.

      So someone who have already used it for the purpose can explain me how in my case I can use template file to make my work easy?

      This will be really very helpful to me like beginners of piklist 🙂

    • #7683
      Steve
      Keymaster

      Try this to get the data:

      foreach ($editor_info as $info => $value) {
      
        echo 'name: ' . $value['editor_name'];
        echo 'image: ' . $value['editor_image'][0];
        echo '<img src="' . wp_get_attachment_url($value['editor_image'][0]) . '"/>';
      
      }

      Just to clarify, this is not piklist-specific. This is standard PHP/WordPress. Piklist does things the PHP/WordPress way, so any Google search on how to loop through an array would help you.

Viewing 1 reply thread
  • You must be logged in to reply to this topic.