Ich habe eine Anforderung, bei der die Anzahl der in der Metabox ausgegebenen Kontrollkästchen nicht festgelegt ist. Wenn es behoben worden wäre, hätte ich jedes der Tutorials im Internet folgen können, in denen vorgeschlagen wird, dass ich jedes Kontrollkästchen benenne und es mit update_post_meta()
speichere.
In meinem Fall habe ich das Kontrollkästchen in der Array-Notation als name = 'multval[]'
bezeichnet, da die Anzahl der Kontrollkästchen unbekannt ist.
Ich verstehe nicht, wie man es benutzt:
get_post_meta()
checked()
update_post_meta()
wenn es darum geht, auf Kontrollkästchen zuzugreifen, wenn es als Array benannt ist.
Der Code:
<?php
add_action( 'add_meta_boxes', function() {
add_meta_box( 'custom-metabox', 'Select values', 'fill_metabox', 'post', 'normal' );
});
function fill_metabox( $post ) {
wp_nonce_field( basename(__FILE__), 'mam_nonce' );
// How to use 'get_post_meta()' for multiple checkboxes as array?
$elements = get_elements(); //returns an associative array
foreach ( $elements as $element) {
?>
<p>
<input
type = "checkbox"
name = "multval[]"
value = <?php echo $element;?>
//How to use checked() function in case of
//multiple checkbox as arrays
>
<?php echo $element;?>
</p>
<?php
}
}
add_action( 'save_post', function( $post_id ) {
$is_autosave = wp_is_post_autosave( $post_id );
$is_revision = wp_is_post_revision( $post_id );
$is_valid_nonce = ( isset( $_POST[ 'mam_nonce' ] ) && wp_verify_nonce( $_POST[ 'mam_nonce' ], basename( __FILE__ ) ) ) ? 'true' : 'false';
if ( $is_autosave || $is_revision || !$is_valid_nonce ) {
return;
}
/*How to run a loop to save values for each checkbox?*/
});
Versuchen Sie den folgenden Code.
add_action( 'add_meta_boxes', function() {
add_meta_box( 'custom-metabox', 'Select values', 'fill_metabox', 'post', 'normal' );
});
function fill_metabox( $post ) {
wp_nonce_field( basename(__FILE__), 'mam_nonce' );
// How to use 'get_post_meta()' for multiple checkboxes as array?
$postmeta = maybe_unserialize( get_post_meta( $post->ID, 'elements', true ) );
// Our associative array here. id = value
$elements = array(
'Apple' => 'Apple',
'orange' => 'Orange',
'banana' => 'Banana'
);
// Loop through array and make a checkbox for each element
foreach ( $elements as $id => $element) {
// If the postmeta for checkboxes exist and
// this element is part of saved meta check it.
if ( is_array( $postmeta ) && in_array( $id, $postmeta ) ) {
$checked = 'checked="checked"';
} else {
$checked = null;
}
?>
<p>
<input type="checkbox" name="multval[]" value="<?php echo $id;?>" <?php echo $checked; ?> />
<?php echo $element;?>
</p>
<?php
}
}
add_action( 'save_post', function( $post_id ) {
$is_autosave = wp_is_post_autosave( $post_id );
$is_revision = wp_is_post_revision( $post_id );
$is_valid_nonce = ( isset( $_POST[ 'mam_nonce' ] ) && wp_verify_nonce( $_POST[ 'mam_nonce' ], basename( __FILE__ ) ) ) ? 'true' : 'false';
if ( $is_autosave || $is_revision || !$is_valid_nonce ) {
return;
}
// If the checkbox was not empty, save it as array in post meta
if ( ! empty( $_POST['multval'] ) ) {
update_post_meta( $post_id, 'elements', $_POST['multval'] );
// Otherwise just delete it if its blank value.
} else {
delete_post_meta( $post_id, 'elements' );
}
});