请尝试此代码。此代码将动态创建元框
<?php
add_action( \'add_meta_boxes\', \'dynamic_add_custom_box\' );
/* Adds a box to the main column on the Post and Page edit screens */
function dynamic_add_custom_box() {
add_meta_box(
\'dynamic_sectionid\',
__( \'Shortcode Information\', \'myplugin_textdomain\' ),
\'dynamic_inner_custom_box\',
\'post\');}
请在上述函数中的post位置添加自定义post的slug名称
/*打印方框内容*/
function dynamic_inner_custom_box() {
global $post;
wp_nonce_field( plugin_basename( __FILE__ ), \'dynamicMeta_noncename\' );
$shortcode = get_post_meta($post->ID,\'shortcode\',true);
$c = 0;
if ( count( $shortcode ) > 0 ) {
if(is_array($shortcode)){
foreach( $shortcode as $track ) {
if ( isset( $track[\'toggle_title\'] ) || isset( $track[\'toggle_title_close\'] ) ) {
printf( \'<div class="myshortcode_div" style="background-color: rgb(223, 223, 223); padding: 10px 20px;margin-bottom: 15px;">
<h1>Shortcode</h1><br/>
Title Open :<br/> <input type="text" name="shortcode[%1$s][toggle_title]" value="%2$s" /><br/>
Title Close :<br/> <input type="text" name="shortcode[%1$s][toggle_title_close]" value="%3$s" /><br/>
<span class="remove" id="remove_shortcode">%8$s</span></div>\', $c, $track[\'toggle_title\'], $track[\'toggle_title_close\'], $track[\'toggle_hide\'], $track[\'toggle_border\'], $track[\'include_excerpt_html\'], $track[\'shortcode_content\'], __( \'Remove Shortcode\' ));
$c = $c +1;
}
}
}
}
?>
<span id="here"></span>
<span class="add" id="add_shortcode"><?php _e(\'Add Shortcode\'); ?></span>
<script>
var $ =jQuery.noConflict();
$(document).ready(function() {
var count = <?php echo $c; ?>;
$(".add").click(function() {
count = count + 1;
$(\'#here\').append(\'<div class="myshortcode_div" style="background-color: rgb(223, 223, 223); padding: 10px 20px;margin-bottom: 15px;">\\n\\
<h1>Shortcode</h1><br/>\\n\\
<b>Title Open :</b><br/> <input type="text" name="shortcode[\'+count+\'][toggle_title]" value="" /><br/>\\n\\
<b>Title Close :</b><br/> <input type="text" name="shortcode[\'+count+\'][toggle_title_close]" value="" /><br/> \\n\\
<span class="remove" id="remove_shortcode">Remove Shortcode</span></div>\' );
return false;
});
$(".remove").live(\'click\', function() {
$(this).parent().remove();
});
});
</script>
/*对输入的数据进行处理*/
add_action( \'save_post\', \'dynamic_save_postdata\' );
/*保存帖子时,保存我们的自定义数据*/
function dynamic_save_postdata( $post_id ) {
if ( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE )
return;
if ( !isset( $_POST[\'dynamicMeta_noncename\'] ) )
return;
if ( !wp_verify_nonce( $_POST[\'dynamicMeta_noncename\'], plugin_basename( __FILE__ ) ) )
return;
$shortcode = $_POST[\'shortcode\'];
update_post_meta($post_id,\'shortcode\',$shortcode);
}
如果此代码段对您有效,请尝试它。