我使用这个问题的所有代码创建了一个元框:
Create more Meta Boxes as needed
单击“添加轨迹”按钮时,基本上只创建两个字段:
Song title field - Track number field
但我需要这样的东西:
添加相册名称按钮-为相册名称添加字段
添加曲目编号按钮-添加曲目编号字段;
这是我现在的代码:
add_action( \'add_meta_boxes\', array( $this, \'dynamic_add_custom_box\' ) );
/* Do something with the data entered */
add_action( \'save_post\', array( $this, \'dynamic_save_postdata\' ) );
第二块:
/* Adds a box to the main column on the Post and Page edit screens */
function dynamic_add_custom_box() {
add_meta_box(
\'dynamic_sectionid\',
__( \'My Albums\', \'myplugin_textdomain\' ),
array( $this,\'dynamic_inner_custom_box\'),
\'post\',
\'normal\',
\'high\');
}
第三块:
/* Prints the box content */
function dynamic_inner_custom_box() {
global $post;
// Use nonce for verification
wp_nonce_field( plugin_basename( __FILE__ ), \'dynamicMeta_noncename\' );
?>
<div id="meta_inner">
<?php
//get the saved meta as an arry
$albums = get_post_meta( $post->ID, \'album\', true );
$c = 0;
if ( count( $albums ) > 0 ) {
foreach( $albums as $album ) {
if ( isset( $album[\'title\'] ) || isset( $album[\'track\'] ) ) {
printf( \'<p>Album name <input type="text" name="album[%1$s][title]" value="%2$s" /> -- Track number : <input type="text" name="album[%1$s][track]" value="%3$s" /><span class="removealbum">%4$s</span></p>\', $c, $album[\'title\'], $album[\'track\'], __( \'Remove Album\' ) );
$c = $c +1;
}
}
}
?>
<span id="here"></span>
<span class="button addalbum"><?php _e(\'Add Album\'); ?></span>
<span class="button addtracks"><?php _e(\'Add Tracks\'); ?></span>
<script>
var $ =jQuery.noConflict();
$(document).ready(function() {
var count = <?php echo $c; ?>;
$(".addalbum").click(function() {
count = count + 1;
$(\'#here\').append(\'<p> Album name <input type="text" name="album[\'+count+\'][title]" value="" /><span class="removealbum">Remove Album</span></p>\' );
return false;
});
$(".addtracks").click(function() {
count = count + 1;
$(\'#here\').append(\'<p> Track number : <input type="text" name="album[\'+count+\'][track]" value="" /><span class="removetrack">Remove Track</span></p>\' );
return false;
});
$(".removealbum").live(\'click\', function() {
$(this).parent().remove();
});
$(".removetrack").live(\'click\', function() {
$(this).parent().remove();
});
});
</script>
</div>
<?php }
最后一块:
/* When the post is saved, saves our custom data */
function dynamic_save_postdata( $post_id ) {
// verify if this is an auto save routine.
// If it is our form has not been submitted, so we dont want to do anything
if ( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE )
return;
// verify this came from the our screen and with proper authorization,
// because save_post can be triggered at other times
if ( !isset( $_POST[\'dynamicMeta_noncename\'] ) )
return;
if ( !wp_verify_nonce( $_POST[\'dynamicMeta_noncename\'], plugin_basename( __FILE__ ) ) )
return;
// OK, we\'re authenticated: we need to find and save the data
$songs = $_POST[\'album\'];
update_post_meta( $post_id, \'album\', $songs );
}
保存任何内容之前:
album NULL
元键(&M);元值正常;
保存内容后:
元值-它们位于不同的数组组中。无法将哪个唱片集与每个曲目编号相关联。
album \'a:2:{i:2;a:1:{s:5:"title";s:22:"Anjunabeats Volume Six";}i:3;a:1:{s:5:"track";s:1:"5";}}\'
我需要的是:
Album[0]
Track number [0]
Track number [1]
Album[1]
Track number [0]
Track number [1]
Track number [2]
Album[2]
Track number [0]
我尝试了很多东西,但我被困在这里:(
任何想法都会有帮助。