我为作者添加了一个自定义元数据库,他们可以在其中填写指向源的url,然后我可以在模板中使用该url。
在我的functions.php
<?php
/* Add metaboxes (see below) */
function add_custom_metaboxes(){
add_meta_box(\'source_post_metabox\', \'Link to bron (optional)\', \'output_source_metabox\', \'post\');
}
add_action(\'add_meta_boxes\', \'add_custom_metaboxes\');
/* Save values of custom metaboxes on save */
function save_custom_metabox($post_id){
if(!isset($_POST[\'source_post_metabox_nonce\'])) :
return;
endif;
if(!wp_verify_nonce( $_POST[\'source_post_metabox_nonce\'], \'source_post_metabox\')) :
return;
endif;
if(defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE) :
return;
endif;
if(!current_user_can(\'edit_post\', $post_id)) :
return;
endif;
if ( isset( $_POST[\'source_post\'] ) ) {
$data = sanitize_text_field( $_POST[\'source_post\'] );
update_post_meta( $post_id, \'_post_source\', $data );
}
}
add_action(\'save_post\', \'save_custom_metabox\');
function output_source_metabox($post){
wp_nonce_field(\'source_post_metabox\', \'source_post_metabox_nonce\');
$post_source = $post->_post_source;
echo \'<label for="source_post">\';
echo \'<input type="text" id="source_post" name="source_post" value="\'.$post_source.\'" style="width: 80%;max-width: 720px;">\';
echo \' Source of your post</label>\';
echo \'<p>Try to be as specific as possible. <br> E.g. <em>http://tweakers.net/nieuws/101372/ing-belgie-wil-betalingsgedrag-van-klanten-meer-gebruiken-voor-dienstverlening.html</em></p>\';
}
?>
但是,我想添加添加多个源的选项。我想了想,最人性化的方法似乎是添加一个按钮,将输入字段添加到metabox中。类似这样:http://jsfiddle.net/BramVanroy/23n6s717/counter = 0;
$(".add-field").click(function() {
counter++;
$("#source_post").after(\'<input type="text" id="source_post_\'+counter+\'" name="source_post_\'+counter+\'" value="" style="width: 80%;max-width: 720px;">\');
});
然而,我不知道如何将这些新输入字段“链接”到后端。如何让Wordpress知道有多个字段需要记帐和保存?我如何才能输出所有这些数据?