我正在为活动制作一个元框。每个活动都有自己的页面,其中包含一个用于排队的元框、背景图像、门票页面URL和一些文本。
为了创建队列,我想使用WP Tuts+“可重用自定义元框教程”中的可重复字段:http://wp.tutsplus.com/tutorials/reusable-custom-meta-boxes-part-1-intro-and-basic-fields/.
但是,我不知道如何在可重复字段中添加多个字段。这就是想法:
[可重复]
-文本字段:艺术家姓名
-文本字段:艺术家网站
-复选框:头条新闻(是或否)
[/repeatable]
这是教程中的代码:
function meta_box_callback($fields, $page) {
global $post;
echo \'<input type="hidden" name="\'.$page.\'_meta_box_nonce" value="\'.wp_create_nonce(basename(__FILE__)).\'" />\';
// Begin the field table and loop
echo \'<table class="form-table cpt">\';
foreach ($fields as $field) {
// get value of this field if it exists for this post
if ($field[\'label\']) $label = $field[\'label\'];
if ($field[\'desc\']) $desc = \'<span class="description">\'.$field[\'desc\'].\'</span>\';
if ($field[\'id\']) $id = $field[\'id\'];
if ($field[\'type\']) $type = $field[\'type\'];
if ($field[\'value\']) $value = $field[\'value\'];
if ($field[\'options\']) $options = $field[\'options\'];
$meta = get_post_meta($post->ID, $id, true);
// begin a table row with
echo \'<tr>
<th class="cpt_title"><label for="\'.$id.\'">\'.$label.\'</label></th>
<td class="cpt_content">\';
switch($field[\'type\']) {
case \'repeatable\':
echo \'<a class="repeatable-add button" href="#">+</a>
<ul id="\'.$field[\'id\'].\'-repeatable" class="custom_repeatable">\';
$i = 0;
if ($meta) {
foreach($meta as $row) {
echo \'<li><span class="sort hndle">|||</span>
<input type="text" name="\'.$field[\'id\'].\'[\'.$i.\']" id="\'.$field[\'id\'].\'" value="\'.$row.\'" size="30" />
<a class="repeatable-remove button" href="#">-</a></li>\';
$i++;
}
} else {
echo \'<li><span class="sort hndle">|||</span>
<input type="text" name="\'.$field[\'id\'].\'[\'.$i.\']" id="\'.$field[\'id\'].\'" value="" size="30" />
<a class="repeatable-remove button" href="#">-</a></li>\';
}
echo \'</ul>
<span class="description">\'.$field[\'desc\'].\'</span>\';
break;
} // switch
echo \'</td></tr>\';
} // foreach
echo \'</table>\'; // table
}
function meta_box_save($post_id, $fields, $page) {
// verify nonce
if (!wp_verify_nonce($_POST[$page.\'_meta_box_nonce\'], basename(__FILE__)))
return $post_id;
// check autosave
if (defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE)
return $post_id;
// check permissions
if ($page != $_POST[\'post_type\']) {
if (!current_user_can(\'edit_page\', $post_id))
return $post_id;
} elseif (!current_user_can(\'edit_post\', $post_id)) {
return $post_id;
}
// loop through fields and save the data
foreach ($fields as $field) {
if($field[\'type\'] == \'tax_select\') {
// save taxonomies
$term = $_POST[$field[\'id\']];
wp_set_object_terms( $post_id, $term, $field[\'id\'] );
}
else {
// save the rest
$old = get_post_meta($post_id, $field[\'id\'], true);
$new = $_POST[$field[\'id\']];
if ($new && $new != $old) {
update_post_meta($post_id, $field[\'id\'], $new);
} elseif (\'\' == $new && $old) {
delete_post_meta($post_id, $field[\'id\'], $old);
}
}
} // foreach
}
希望你们能帮帮我:-)。
SO网友:Matthew Boynes
尝试Fieldmanager. 它是以重复组为优先级#1构建的。您的新代码如下所示:
add_action( \'init\', function() {
$fm = new Fieldmanager_Group( array(
\'name\' => \'artists\',
\'limit\' => 0,
\'label\' => \'New Artist\',
\'label_macro\' => array( \'Artist: %s\', \'name\' ),
\'add_more_label\' => \'Add another Artist\',
\'children\' => array(
\'name\' => new Fieldmanager_Textfield( \'Name\' ),
\'website\' => new Fieldmanager_Link( \'Website\' ),
\'headliner\' => new Fieldmanager_Checkbox( \'Headliner\' )
)
) );
$fm->add_meta_box( \'Artists\', array( \'post\' ) );
} );
披露:我为开源公司工作的这个插件