0

I would like to display metabox repeater fields on the frontend.

Here is the code to setup the metabox repeater fields:

$my_meta =  new AT_Meta_Box($config);  

$repeater_fields[] = $my_meta->addText($prefix.'t_field_id',array('name'=> 'Title'),true);
$repeater_fields[] = $my_meta->addText($prefix.'d_field_id',array('name'=> 'Description'),true);
$repeater_fields[] = $my_meta->addText($prefix.'p_field_id',array('name'=> 'Price'),true);
$repeater_fields[] = $my_meta->addImage($prefix.'i_field_id',array('name'=> 'Image'),true);
$my_meta->addRepeaterBlock($prefix.'re_',array('inline' => true, 'name' => 'This is a Repeater Block','fields' => $repeater_fields));      
$my_meta->Finish();

I tried to use this code:

$title = get_post_meta(get_the_ID(),'mt_t_field_id',true); 
                    echo $title;

Any advice?

1

Nice to see people using my class :)

Anyway when you save a field in a repeater block , to access the data you need to use the repeater ID which in your case it's

   $repeater_data = get_post_meta(get_the_ID(),$prefix.'re_',true);

and this returns an array of arrays with the id as key and value as value so you can loop over the array like this:

foreach($repeater_data as $arr){
   echo 'title: ' . $arr[$prefix.'t_field_id'];
   echo '<br />Description: ' . $arr[$prefix.'d_field_id'];
   echo '<br />Price: ' . $arr[$prefix.'p_field_id'];
   echo '<br />image: <img src="' . $arr[$prefix.'i_field_id']['url']">';
}

to understand better how an image field is stored take a look at the class's wiki or the comments on this post

  • just a little question, why the foreach data return an invalid argument supplied... $repeater_data is an array ? – wordpress nooby Apr 2 '12 at 15:14
  • I can't tell without seeing your code, but you can simply do a var_dump($repeater_data); once to see whats going on there. – Bainternet Apr 2 '12 at 15:21
  • ok all is fine i was make a mistake with the $prefix... so noob :) all apologies and thanks again sir – wordpress nooby Apr 2 '12 at 15:27
  • Glad it worked out for you – Bainternet Apr 2 '12 at 15:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.