你的意思是這樣的嗎?
add_action('add_meta_boxes', 'dynamic_add_custom_box');
/* Do something with the data entered */
add_action('save_post', '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 Slogans', 'myplugin_textdomain'),
'dynamic_inner_custom_box',
'post');
}
/* Render the box content */
function dynamic_inner_custom_box() {
global $post;
// nonce for verification
wp_nonce_field(plugin_basename(__FILE__), 'dynamicMeta_noncename');
?>
<div id="meta_inner">
<?php
//GEt the array of saved meta
$slogans = get_post_meta($post->ID,'slogans',true);
$c = 0;
//if (count($slogans) > 0) {
if(is_array ($slogans)){
foreach($slogans as $slogan) {
if (isset($slogan['name']) || isset($slogan['slogan'])) {
printf('<p>Slogan Name <input type="text" name="slogans[%1$s][name]" value="%2$s" /> -- Slogan Content : <input type="text" name="slogans[%1$s][slogan]" value="%3$s" /><input class="button tagadd remove" type="button" value="%4$s"></p>', $c, $slogan['name'], $slogan['slogan'], __('Remove Slogan'));
$c = $c +1;
}
}
}
?>
<span id="here"></span>
<input class="button tagadd add" type="button" value="<?php _e('Add Slogan'); ?>">
<script>
var $ =jQuery.noConflict();
$(document).ready(function() {
var count = <?php echo $c; ?>;
$(".add").click(function() {
count = count + 1;
$('#here').append('<p> Slogan Name <input type="text" name="slogans['+count+'][name]" value="" /> -- Slogan Content : <input type="text" name="slogans['+count+'][slogan]" value="" /><input class="button tagadd remove" type="button" value="<?php _e('Remove Slogan'); ?>">');
return false;
});
$(".remove").live('click', function() {
$(this).parent().remove();
});
});
</script>
</div><?php
}
/* saves our custom data when the post is saved */
function dynamic_save_postdata($post_id) {
// verify if this is an auto save routine.
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
return;
// verify Nonce and that the request is valid,
if (!isset($_POST['dynamicMeta_noncename']))
return;
if (!wp_verify_nonce($_POST['dynamicMeta_noncename'], plugin_basename(__FILE__)))
return;
// GOOD; we are set, find a save data
$slogans = $_POST['slogans'];
update_post_meta($post_id,'slogans',$slogans);
}
是的,但它應該有一個額外的選項頁面,而不是在一個新的職位,這可能嗎? – maddo7 2013-02-25 18:30:47
確定它可以做到,你只需要使用wp Options API [http://codex.wordpress.org/Options_API]創建你的選項頁面,然後使用相關的部分(jQuery和form),用post_meta通過設置功能(例如'get_post_meta' - >'get_option')等功能。 – 2013-02-26 03:21:34