Is there any way to have a registered meta box dynamically appear numerous times based what is selected/entered in another meta box or taxonomy field?
i.e. my custom post needs to have a variable amount of "sections" (text fields). There would be no way for me to pre-define how many that would be; only during data entry can that amount be known. These fields would then be also dynamically added to the post template on display.
After registering my custom post also called post and a metabox with callback "display_post_meta_box" this worked:
function display_post_meta_box( $post ) {
// Retrieve current details of the post based on post ID
$section_number = get_post_meta( $post->ID, 'section_number', true ) ;
/*Configuration for post section WYSIWYG editors*/
$i = 1;
while ($i <= $section_number) {
$section_editor_settings[$i] = array( 'textarea_rows' => '5','media_buttons' => false, 'textarea_name'=>__( 'post_section'.$i ), 'teeny'=>'true' );
$i++; }
// Retrieve current details of the post based on post ID
$section = get_post_meta( $post->ID, 'section', true );
$section_number = get_post_meta( $post->ID, 'section_number', true ) ;
?>
<style>.cell{display: table-cell; vertical-align: middle;}
.row{width: 100%;display: table;border-bottom: 1px solid #DFDFDF;padding: 2px;}
</style>
<div class="row">
<div class="cell" style="width: 15%"><b>Number of sections</b></div>
<div class="cell" style="width: 10%"><input type="number" name="post_section_number" value="<?php echo $section_number; ?>" /></input></div>
<div class="cell" style="width: 5%"> <script>
jQuery('.metabox_submit').click(function(e) {
e.preventDefault();
jQuery('#publish').click();
});
</script>
<input type="submit" class="metabox_submit button button-primary" value="Submit" /></div>
<div class="cell" style="width: 70 %"> </div>
</div>
<?php
$i = 1;
while ($i <= $section_number) {?>
<div class="row">
<div class="cell" style="width: 30%"><b>Section <?php echo '#'.$i;?> </b></div>
<div class="cell" style="width: 50%"><?php wp_editor( $section[$i], 'sectionid'.$i, $section_editor_settings[$i] ); ?> </div>
<div class="cell" style="width: 5%"></div>
<div class="cell" style="width: 15%"><p class="howto">Enter body for this section here, and apply neccessary formatting</p></div>
</div>
<?php $i++; }
}
/Saves Post/
add_action( 'save_post', 'add_post_fields', 10, 2 );
function add_post_fields( $post_id, $post ) {
// Check post type for movie reviews
$section_values[] = array();
if ( $post->post_type == 'post' ) {
$i = 1;
while ($i <= 4) {
$section_values[$i] = $_POST['post_section'.$i];
$i++; }
// Store data in post meta table if present in post data
if ( isset( $section_values ) && $section_values != '' ) {
update_post_meta( $post_id, 'section', $section_values );
}
if ( isset( $_POST['post_section_number'] ) && $_POST['post_section_number'] != '' ) {
update_post_meta( $post_id, 'section_number', $_POST['post_section_number'] );
}
}
}
I don't know if you're asking for specific code to do the above, but if its more of a general question I would recommend the Types plugin
It has loads of features, but it lets you choose what post types groups of custom fields appear on, what page templates they appear on, and, importantly for your question, they also have conditional display based on other field values. I'm not sure how useful this might be without a slightly more specific question, and I'm sorry its not a code-y answer, but hope it helps.