I’m building a quite complex custom post type structure and I would need some help regarding my metaboxes.
What I want to do:
- Thank’s to the
have_fields_and_multi()
function, the user enters data in simple text input fields (with a “Add new” button)
- The values from the previous text inputs should be used to build a
select
dropdown in another metabox.
To make it simple, here is a mockup (also attached to this post): http://idzr.org/0c95
I have the first part working, it’s easy. But I can’t figure out how to make the second part to work. If I use a while($mb->have_fields_and_multi('aaa'))
in another while($mb->have_fields_and_multi('bbb'))
the page is infinite (the loop doesn’t end. If I use foreach
I have other problems.
Do you have an idea about how I can achieve this ? Thanks!!!
Ok, I finally managed to solve that by myself. It is possible thanks to :
-
Create the first field :
<?php
while($mb->have_fields_and_multi('types')):
$mb->the_group_open();
$mb->the_field('type');
?>
<input type="text" id="<?php $mb->the_name(); ?>" name="<?php $mb->the_name(); ?>" value="<?php $mb->the_value(); ?>" />
<a href="#" class="dodelete button">Remove</a>
<?php
$mb->the_group_close();
endwhile;
?>
<a href="#" class="docopy-types button" style="float: left">Add new</a>
<a href="#" class="dodelete-types button" style="float: right">Delete all</a>
-
Create the second batch of fields while using foreach
to get the data from the first fields and put that in a select
:
<?php
while($mb->have_fields_and_multi('details')):
$mb->the_group_open();
$mb->the_field('detail_select');
?>
<select name="<?php $mb->the_name(); ?>">
<option value="">Choose...</option>
<?php foreach ($meta['types'] as $types) { ?>
<option value="<?php echo $types['type']; ?>"<?php $mb->the_select_state($types['type']); ?>><?php echo $types['type']; ?></option>
<?php } ?>
</select>
<?php $mb->the_field('detail_title'); ?>
<label>Description</label>
<input type="text" id="<?php $mb->the_name(); ?>" name="<?php $mb->the_name(); ?>" value="<?php $mb->the_value(); ?>" /><br />
<a href="#" class="dodelete button">Remove</a>
<?php
$mb->the_group_close();
endwhile;
?>
<a href="#" class="docopy-estate_details button">Add new</a>
<a href="#" class="dodelete-estate_details button">Delete all</a>