我正试图互相引用帖子。编辑应具有特定帖子标题的自定义字段选择列表,以将当前帖子与其他帖子链接。我想我已经走得很远了。选择列表将正确显示。所有标题都在那里。
现在,我被困在了必须保存被引用帖子ID的地方。
如何获取所选选项的值、保存并将其设为默认值?是否必须将所有内容都封装在表单函数中?
这就是我目前的情况:
add_action("admin_init", "admin_init");
add_action(\'save_post\', \'save_reference_id\');
/*** add_meta_box ***/
function admin_init(){
// adding a custom field to post type \'case\'
add_meta_box("refInfo-meta", "Reference", "meta_options", "case", "side", "high");
}
/*** callback ***/
function meta_options(){
?>
<form action="<?php bloginfo(\'url\'); ?>" method="get">
<select name="page_id" id="page_id">
<?php
global $post;
// getting all child pages of ID 21
$args = array( \'numberposts\' => -1, \'post_type\' => \'page\', \'post_parent\' => 21);
$posts = get_posts($args);
foreach( $posts as $post ) : setup_postdata($post); ?>
<option value="<?php echo $post->ID; ?>"><?php the_title(); ?></option>
<?php endforeach; ?>
</select>
</form>
<?php
}
/*** save_post ***/
function save_reference_id(){
// big question mark
}
最合适的回答,由SO网友:leymannx 整理而成
selected()
对于设置默认值有很大帮助。我在这篇精彩的元盒教程中找到了其余内容:http://code.tutsplus.com/tutorials/how-to-create-custom-wordpress-writemeta-boxes--wp-20336 文本输入、复选框和下拉列表示例。而且Custom post type's slug gets wrong when adding a custom meta box 解释了如何正确处理当前的post对象,使其不会与options对象混淆。
/*** callback ***/
function meta_options(){
global $post;
// storing the global post object so it doesn\'t get mixed up with the options
$post_old = $post
$custom = get_post_custom($post->ID);
if (isset($custom["reference_id"][0])) {
$reference_id = $custom["reference_id"][0];
} else {
$reference_id = \'0\';
}
?>
<form action="<?php bloginfo(\'url\'); ?>" method="get">
<select name="ref_id" id="ref_id">
<option value="0" <?php selected($reference_id, \'0\'); ?>>- choose client -</option>
<?php
global $post;
$args = array(
\'numberposts\' => -1,
\'post_type\' => \'page\',
\'post_parent\' => 21
);
$posts = get_posts($args);
foreach( $posts as $post ) : setup_postdata($post); ?>
<option value="<?php echo $post->ID; ?>" <?php selected($reference_id, $post->ID); ?>><?php the_title(); ?></option>
<?php endforeach; ?>
</select>
</form>
<?php
// restoring the global post object
$post = $post_old;
setup_postdata( $post );
}
/*** save_post ***/
function save_reference_id(){
global $post;
if (isset($_POST["ref_id"])) {
update_post_meta($post->ID, "reference_id", $_POST["ref_id"]);
}
}
SO网友:Mitul
add_action("admin_init", "admin_init");
add_action(\'save_post\', \'save_reference_id\');
/*** add_meta_box ***/
function admin_init(){
// adding a custom field to post type \'case\'
add_meta_box("refInfo-meta", "Reference", "meta_options", "case", "side", "high");
}
/*** callback ***/
function meta_options(){
?>
<select name="page_id" id="page_id">
<?php
global $post;
// getting all child pages of ID 21
$args = array( \'numberposts\' => -1, \'post_type\' => \'page\', \'post_parent\' => 21);
$posts = get_posts($args);
foreach( $posts as $post ) : setup_postdata($post); ?>
<option value="<?php echo $post->ID; ?>"><?php the_title(); ?></option>
<?php endforeach; ?>
</select>
<?php
}
/*** save_post ***/
function save_reference_id($post_id){
update_post_meta($post_id, "[meta_key]", $_POST[\'page_id\']);
}