如何将表添加到自定义帖子类型作为wordpress中的自定义字段
问题描述:
嗨我已经尝试过Tablepress,wp_table_reloaded插件。我觉得对于天真的用户来说,通过为每个自定义帖子的内容添加短代码来添加表格将会非常复杂,同时网页中也有很多自定义帖子。实际上,我希望该表应该添加到每个自定义帖子类型,因为我们将自定义字段添加到每个自定义帖子类型,然后只需将值添加到每个字段。请帮帮我!!!如何将表添加到自定义帖子类型作为wordpress中的自定义字段
答
看看在add_meta_box
功能: http://codex.wordpress.org/Function_Reference/add_meta_box
这是我在最近的一个项目中使用的代码,我相信你应该能够适应元框中添加一个表。
add_action('admin_menu', 'create_meta_boxes');
function create_meta_boxes() {
add_meta_box('author_info', 'Auteur info', 'author_info_meta_box', 'portfolio', 'normal');
}
function author_info_meta_box($object, $box) { ?>
<p>
<label for="auteur-meta">Auteur</label><br />
<input type="text" name="auteur-meta" id="auteur-meta" style="width:100%;" value="<?php echo wp_specialchars(get_post_meta($object->ID, 'Auteur', true), 1); ?>" />
</p>
<p>
<label for="auteur-quote-meta">Quote auteur</label><br />
<input type="text" name="auteur-quote-meta" id="auteur-quote-meta" style="width:100%;" value="<?php echo wp_specialchars(get_post_meta($object->ID, 'Auteur Quote', true), 1); ?>" />
</p>
<p>
<label for="auteurBioMeta">Biografie auteur</label><br />
<?php $settings = array(
'media_buttons' => false,
'textarea_rows' => 6
); ?>
<?php wp_editor(wp_specialchars(get_post_meta($object->ID, 'Auteur Biografie', true), 1), 'auteurBioMeta' , $settings); ?>
</p>
<p>
<label for="order-mail-meta">E-mail voor bestellingen</label><br />
<input type="text" name="order-mail-meta" id="order-mail-meta" style="width:100%;" value="<?php echo wp_specialchars(get_post_meta($object->ID, 'Order Mail', true), 1); ?>" />
</p>
<?php }
//Insert values on save
add_action('save_post', 'save_post', 10, 2);
function save_post($post_id, $post) {
if (!current_user_can('edit_post', $post_id))
return $post_id;
update_post_meta($post_id, 'Auteur', stripslashes($_POST['auteur-meta']));
update_post_meta($post_id, 'Auteur Biografie', stripslashes($_POST['auteurBioMeta']));
update_post_meta($post_id, 'Auteur Quote', stripslashes($_POST['auteur-quote-meta']));
update_post_meta($post_id, 'Order Mail', stripslashes($_POST['order-mail-meta']));
}