英文:
Multiple checkbox in metabox WordPress
问题
这是你的代码中需要翻译的部分:
//"id" => "value"
$expertise_checkbox_elements = array(
'design_research' => pll__('Design research'),
'product_design' => pll__('Product design'),
'3d_modelling' => pll__('3D modelling'),
'engineering' => pll__('Engineering'),
'dfm' => pll__('Design for manufacturing'),
'prototyping' => pll__('Prototyping'),
'production' => pll__('Production')
);
// Save
add_action( 'save_post', 'client_company_save_postdata' );
function client_company_save_postdata( $post_id ) {
$is_revision = wp_is_post_revision( $post_id );
$is_valid_nonce = ( isset( $_POST[ 'expertise_checkbox_field_noncename' ] ) && wp_verify_nonce( $_POST[ 'expertise_checkbox_field_noncename' ], basename( __FILE__ ) ) ) ? 'true' : 'false';
if ( $is_revision || !$is_valid_nonce )
return;
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
return;
if( ! current_user_can( 'edit_post', $post_id ) )
return;
if ( ! empty( $_POST['multval'] ) ) {
update_post_meta( $post_id, 'expertise_checkbox_elements', $_POST['multval'] );
// Otherwise just delete it if its blank value.
} else {
delete_post_meta( $post_id, 'expertise_checkbox_elements' );
}
}
// Output values
<?php
$expertise_elements = (get_post_meta( $post->ID, 'expertise_checkbox_elements', true ));
foreach ($expertise_elements as $element) {
echo ('<li>'.$element.'</li>');
}
?>
这里是翻译后的内容:
// "id" => "value"
$expertise_checkbox_elements = array(
'design_research' => pll__('Design research'),
'product_design' => pll__('Product design'),
'3d_modelling' => pll__('3D modelling'),
'engineering' => pll__('Engineering'),
'dfm' => pll__('Design for manufacturing'),
'prototyping' => pll__('Prototyping'),
'production' => pll__('Production')
);
// 保存
add_action( 'save_post', 'client_company_save_postdata' );
function client_company_save_postdata( $post_id ) {
$is_revision = wp_is_post_revision( $post_id );
$is_valid_nonce = ( isset( $_POST[ 'expertise_checkbox_field_noncename' ] ) && wp_verify_nonce( $_POST[ 'expertise_checkbox_field_noncename' ], basename( __FILE__ ) ) ) ? 'true' : 'false';
if ( $is_revision || !$is_valid_nonce )
return;
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
return;
if( ! current_user_can( 'edit_post', $post_id ) )
return;
if ( ! empty( $_POST['multval'] ) ) {
update_post_meta( $post_id, 'expertise_checkbox_elements', $_POST['multval'] );
// 否则,如果它是空值,就删除它。
} else {
delete_post_meta( $post_id, 'expertise_checkbox_elements' );
}
}
// 输出值
<?php
$expertise_elements = (get_post_meta( $post->ID, 'expertise_checkbox_elements', true ));
foreach ($expertise_elements as $element) {
echo ('<li>'.$element.'</li>');
}
?>
希望这有所帮助!如果你有其他问题或需要进一步的翻译,请随时告诉我。
英文:
I'm making multiple checkboxes in post admin panel. And when user create post and check necessary checkbox it stored it to output in post page.
My code for creating and saving checkboxes.
function expertise_checkbox_meta_box_callback( $post, $meta ){
$screens = $meta['args'];
wp_nonce_field( plugin_basename(__FILE__), 'expertise_checkbox_field_noncename' );
$value = maybe_unserialize(get_post_meta( $post->ID, 'expertise_checkbox_elements', true ));
//"id" => "value"
$expertise_checkbox_elements = array(
'design_research' => pll__('Design research'),
'product_design' => pll__('Product design'),
'3d_modelling' => pll__('3D modelling'),
'engineering' => pll__('Engineering'),
'dfm' => pll__('Design for manufacturing'),
'prototyping' => pll__('Prototyping'),
'production' => pll__('Production')
);
foreach ( $expertise_checkbox_elements as $id => $element ) {
// If the value for checkboxes exist and
// this element is part of saved meta check it.
if ( is_array( $value ) && in_array( $id, $value ) ) {
$checked = 'checked="checked"';
} else {
$checked = null;
}
?>
<p>
<input type="checkbox" name="multval[]" value="<?php echo $id;?>" <?php echo $checked; ?> />
<?php echo $element;?>
<br>
</p>
<?php
}
}
##Save
add_action( 'save_post', 'client_company_save_postdata' );
function client_company_save_postdata( $post_id ) {
$is_revision = wp_is_post_revision( $post_id );
$is_valid_nonce = ( isset( $_POST[ 'expertise_checkbox_field_noncename' ] ) && wp_verify_nonce( $_POST[ 'expertise_checkbox_field_noncename' ], basename( __FILE__ ) ) ) ? 'true' : 'false';
if ( $is_revision || !$is_valid_nonce )
return;
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
return;
if( ! current_user_can( 'edit_post', $post_id ) )
return;
if ( ! empty( $_POST['multval'] ) ) {
update_post_meta( $post_id, 'expertise_checkbox_elements', $_POST['multval'] );
// Otherwise just delete it if its blank value.
} else {
delete_post_meta( $post_id, 'expertise_checkbox_elements' );
}
}
And this code worked (it's save checkbox status and update it). But when I output array of the checkbox values at post page I see design_research
instead of "Design research", dfm
instead of "Design for manufacturing" and etc.
Code for output values.
<?php
$expertise_elements = (get_post_meta( $post->ID, 'expertise_checkbox_elements', true ));
foreach ($expertise_elements as $element) {
echo ('<li>'.$element.'</li>');
}
?>
I see that I store "keys" from array, not values. But I can't understand how to make this code output "values" from array.
答案1
得分: 1
您可以将数组的键设置为用户友好的,但更合适的做法是在一个函数中定义选项数组,然后在后端和前端都使用该函数。
英文:
See the answer I provided here:
https://wordpress.stackexchange.com/questions/416533/get-custom-field-label-select-dropdown-on-front-end/416534#416534
> You can set the keys of the array to be user-friendly, but more appropriate would be to define the options array in a function, and then use that function in both the backend and frontend.
答案2
得分: 1
Sure, here is the translated code part:
只需使用最初的数组,作为名称的映射。类似这样:
$names = array(
'design_research' => __('Design research'),
'product_design' => __('Product design'),
'3d_modelling' => __('3D modelling'),
'engineering' => __('Engineering'),
'dfm' => __('Design for manufacturing'),
'prototyping' => __('Prototyping'),
'production' => __('Production')
);
$expertise_elements = (get_post_meta($post->ID, 'expertise_checkbox_elements', true));
foreach ($expertise_elements as $el) {
echo ('<li>' . $names[$el] . '</li>');
}
If you need any further assistance, please let me know.
英文:
Just use the same array you had initially, as a map to names.
Something like this:
$names = array(
'design_research' => __('Design research'),
'product_design' => __('Product design'),
'3d_modelling' => __('3D modelling'),
'engineering' => __('Engineering'),
'dfm' => __('Design for manufacturing'),
'prototyping' => __('Prototyping'),
'production' => __('Production')
);
$expertise_elements = (get_post_meta($post->ID, 'expertise_checkbox_elements', true));
foreach ($expertise_elements as $el) {
echo ('<li>' . $names[$el] . '</li>');
}
Depending on the rest of your code, and to not repeat yourself, you can have that array as class variable, a function or a user-provided list of values/names.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论