admin管理员组文章数量:1134597
I have a number custom field with a selector, I should select the number of bedrooms, when I edit the post the meta key value is saved correctly (I can see the number of bedrooms near the correspondent icon on the post) but if I reload the post editor page...the selection has disappeared, how can I fix the following code?
add_action( 'add_meta_boxes_post', "camere_add_meta_box" );
function camere_add_meta_box(){
add_meta_box(
"camere_meta_box", // An ID for the Meta Box. Make it unique to your plugin
__( "Numero camere da letto", 'textdomain' ), // The title for your Meta Box
"camere_meta_box_render", // A callback function to fill the Meta Box with the desired UI in the editor
"post", // The screen name - in this case the name of our custom post type
"side" // The context or placement for the meta box. Choose "normal", "side" or "advanced"
);
}
function camere_meta_box_render( $post ){
$number = get_post_meta( $post->ID, "function_camere", true );
echo '
<div>
<select name="function_camere">';
for( $i = 0; $i < 13; $i++ ) {
echo '<option value="'.$i.'">'.$i.'</option>';
}
echo '
</select>
</div>';
}
add_action( 'save_post', 'camere_meta_box_save');
function camere_meta_box_save( $post_id ) {
if ($_POST['post_type'] == "post") {
$number = $_POST['function_camere'];
update_post_meta( $post_id, "function_camere", $number );
}
}
I have a number custom field with a selector, I should select the number of bedrooms, when I edit the post the meta key value is saved correctly (I can see the number of bedrooms near the correspondent icon on the post) but if I reload the post editor page...the selection has disappeared, how can I fix the following code?
add_action( 'add_meta_boxes_post', "camere_add_meta_box" );
function camere_add_meta_box(){
add_meta_box(
"camere_meta_box", // An ID for the Meta Box. Make it unique to your plugin
__( "Numero camere da letto", 'textdomain' ), // The title for your Meta Box
"camere_meta_box_render", // A callback function to fill the Meta Box with the desired UI in the editor
"post", // The screen name - in this case the name of our custom post type
"side" // The context or placement for the meta box. Choose "normal", "side" or "advanced"
);
}
function camere_meta_box_render( $post ){
$number = get_post_meta( $post->ID, "function_camere", true );
echo '
<div>
<select name="function_camere">';
for( $i = 0; $i < 13; $i++ ) {
echo '<option value="'.$i.'">'.$i.'</option>';
}
echo '
</select>
</div>';
}
add_action( 'save_post', 'camere_meta_box_save');
function camere_meta_box_save( $post_id ) {
if ($_POST['post_type'] == "post") {
$number = $_POST['function_camere'];
update_post_meta( $post_id, "function_camere", $number );
}
}
Share
Improve this question
edited Aug 31, 2023 at 4:22
YourManDan
4342 silver badges12 bronze badges
asked Aug 21, 2023 at 13:22
Andrea SacconiAndrea Sacconi
797 bronze badges
1 Answer
Reset to default 1There's nothing in your code that would allow this to happen. In your HTML you need to add a selected
attribute to the <option>
element representing the current value. WordPress includes a helper function, selected()
which can help with this:
function camere_meta_box_render( $post ){
$number = get_post_meta( $post->ID, "function_camere", true );
echo '<div><select name="function_camere">';
for( $i = 0; $i < 13; $i++ ) {
echo '<option value="'.$i.'" ' . selected( $i, $number, false ) . '>'.$i.'</option>';
}
echo '</select></div>';
}
本文标签: Why the value of the selector doesnt remain visible in the custom field after I edit the post
版权声明:本文标题:Why the value of the selector doesnt remain visible in the custom field after I edit the post? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736821645a1954310.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论