我正在开发一个插件。有两个CPT:类别和菜肴。在我的编辑盘页面中,我尝试使用get_post_meta($post->ID, \'dish_price\', true)
问题是,我用来获取正在编辑的菜肴ID的任何函数都会返回在类别下拉列表中检索到的最后一个类别的ID。我做错了什么?
function meta_boxes(){
/*Category meta boxes */
remove_meta_box(\'commentstatusdiv\', \'category\', \'normal\');
remove_meta_box(\'commentsdiv\', \'category\', \'normal\');
remove_meta_box( \'postexcerpt\', \'category\', \'core\' );
add_meta_box( \'description\', \'Description\', \'category_description_meta_box\', \'category\' );
/* Dish meta boxes */
remove_meta_box(\'commentstatusdiv\', \'dish\', \'normal\');
remove_meta_box(\'commentsdiv\', \'dish\', \'normal\');
remove_meta_box( \'postexcerpt\', \'dish\', \'core\' );
add_meta_box( \'parent_id\', \'Category\', \'dish_category_metabox\', \'dish\' );
add_meta_box( \'description\', \'Description\', \'dish_description_meta_box\', \'dish\' );
add_meta_box(\'dish_price\', \'Price\', \'dish_price_meta_box\', \'dish\');
}
function category_description_meta_box(){
global $post;
//var_dump($post)
echo \'<input type="hidden" name="category_noncename" id="category_noncename" value="\' .wp_create_nonce( plugin_basename(__FILE__) ) . \'" />\';
echo \'<textarea style="width: 100%;" rows="5" name="description" id="description">\'.get_post_meta($post->ID, \'description\', true).\'</textarea>\';
}
function dish_description_meta_box(){
global $post;
//var_dump($post)
echo \'<input type="hidden" name="dish_noncename" id="dish_noncename" value="\' .wp_create_nonce( plugin_basename(__FILE__) ) . \'" />\';
echo \'<textarea style="width: 100%;" rows="5" name="description" id="description">\'.get_post_meta($post->ID, \'description\', true).\'</textarea>\';
}
function dish_price_meta_box(){
global $post;
echo \'$<input type="text" name="dish_price" id="dish_price" value="\'.get_post_meta($post->ID, \'dish_price\', true).\'">\';
}
function dish_category_metabox()
{
global $post;
$out = \'<select name="parent_id">\'.PHP_EOL;
$args = array(
\'post_type\' => \'category\',
\'order\' => \'ASC\'
);
$the_query = new WP_Query($args);
if ($the_query->have_posts()) {
while ($the_query->have_posts()) {
$the_query->the_post();
$out .= \'<option value="\'.get_the_ID().\'"\';
if(get_post_meta($post->ID, \'parent_id\', true) == get_the_ID()){
$out .= \' SELECTED\';
}
$out .= \'>\'.get_the_title().\'</option>\'.PHP_EOL;
}
}
$out .= \'</select>\'.PHP_EOL;
echo $out;
}
add_action(\'add_meta_boxes\', \'meta_boxes\');