【发布时间】:2017-02-15 11:57:57
【问题描述】:
我正在学习如何将元框添加到帖子中。我想创建一组带有文本输入和多个复选框的元框。现在复选框只是像这样放在那里,但最终它们将由一个 foreach 循环生成,其中包含来自另一个地方的内容,所以对我来说给它们起名字是很重要的,比如 entry[0]、entry[1] 等等。它们必须通过循环保存,因为我不知道会生成多少。
这是我目前所拥有的:
// adding the metaboxes
function add_post_reference() {
add_meta_box('post-reference', 'Reference', 'referenceCallBack', 'languagecourses', 'side', 'high');
}
add_action('add_meta_boxes', 'add_post_reference');
// callback function
function referenceCallBack($post) {
wp_nonce_field( 'reference_meta_box', 'reference_nonce' );
$name_value = get_post_meta( $post->ID, '_post_reference_name', true );
$link_value = get_post_meta( $post->ID, '_post_reference_link', true );
尝试对我的复选框执行与上述相同的操作,但我不知道该放什么:
$teachers_value = get_post_meta( $post->ID, 'what do I put here?', true ); // what do I put here?
现在呼应 html 结构(文本输入工作(值被保存),我试图弄清楚如何使复选框也保存:
echo '<label for="reference-name">'. 'Reference Name' .'</label>';
echo '<input type="text" id="reference-name" name="post_reference_name" placeholder="Example" value="'.$name_value.'" size="25"/>';
echo '<p class="howto">'. 'Add the name of the reference' .'</p>';
echo '<label for="reference-link">'. 'Reference Link' .'</label>';
echo '<input type="text" id="reference-link" name="post_reference_link" placeholder="http://www.example.com/" value="'.$link_value.'" size="25"/>';
echo '<p class="howto">'. 'Add the link of the reference' .'</p>';
// my checkboxes
echo '<input type="checkbox" name="entry[0]" value="moredata">';
echo '<input type="checkbox" name="entry[1]" value="moredata">';
echo '<input type="checkbox" name="entry[2]" value="moredata">';
echo '<input type="checkbox" name="entry[3]" value="moredata">';
echo '<input type="checkbox" name="entry[4]" value="moredata">';
}
function save_post_reference( $post_id ) {
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return;
}
if ( ! isset( $_POST['reference_nonce'] ) ) {
return;
}
if ( ! wp_verify_nonce( $_POST['reference_nonce'], 'reference_meta_box' ) ) {
return;
}
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
if ( ! isset( $_POST['post_reference_name'] ) || ! isset( $_POST['post_reference_link'] ) ) {
return;
}
$reference_name = sanitize_text_field( $_POST['post_reference_name'] );
$reference_link = sanitize_text_field( $_POST['post_reference_link'] );
// looping through the checkboxes
for ($i = 0; $i < 5; $i++) {
$teachers_names = sanitize_text_field($_POST['entry'][$i]);
}
update_post_meta( $post_id, '_post_reference_name', $reference_name );
update_post_meta( $post_id, '_post_reference_link', $reference_link );
再说一遍,我在这里放什么?
update_post_meta( $post_id, 'whatdoIputhere?', $teachers_names); // what do I put here?
}
add_action( 'save_post', 'save_post_reference' );
有人可以帮我吗?
【问题讨论】:
-
简化您的元密钥.....使用
get_post_meta($post_ID, 'your key', true);获取数据并使用update_post_meta( $post_id, 'your key', $teachers_names);保存数据 -
我试图弄清楚元键到底是什么,前两个我看到他们使用输入字段的名称,如下所示:
update_post_meta( $post_id, '_post_reference_name', $reference_name ); update_post_meta( $post_id, '_post_reference_link', $reference_link );所以我输入了“entry[]”在这里,但它不保存。update_post_meta( $post_id, 'entry[]', $teachers_names);你能在这里多解释一下吗? -
update_post_meta( $post_id, 'entry', $teachers_names)这就够了 -
谢谢,但很遗憾仍未保存。
标签: php wordpress post checkbox meta-boxes