HTML 形式的关联数组使用 WordPress post 元数据函数
Associative arrays in HTML forms using WordPress post meta data functions
我有一个对象集合需要 saved/retrieved 在 wp_postmeta table 中属于自定义 post 类型。
示例结构:
array(
array(
'firstname' => 'Johnny',
'middlename' => 'William'
),
array(
'firstname' => 'Jane',
'middlename' => 'Alice'
)
)
我希望能够像这样遍历对象:
$children = get_post_meta( $postid, '_children', true);
$arrlength = count($children);
for($x = 0; $x < $arrlength; $x++)
{
echo '<input type="text" name="_children[][firstname]" id="_children[][firstname]" value="' . $meta_values['_children'][0][$x][firstname] . '" /><br />';
echo '<input type="text" name="_children[][middlename]" id="_children[][middlename]" value="' . $meta_values['children'][0][$x][middlename] . '" /><br />';
}
我认为以上说法不正确。我正在尝试通过以下方式获取 posted 数据保存在 save_post
操作中:
function test_meta_save( $post_id ) {
// Checks save status
$is_autosave = wp_is_post_autosave( $post_id );
$is_revision = wp_is_post_revision( $post_id );
$is_valid_nonce = ( isset( $_POST[ '_children_nonce' ] ) && wp_verify_nonce( $_POST[ '_children_nonce' ], basename( __FILE__ ) ) ) ? 'true' : 'false';
// Exits script depending on save status
if ( $is_autosave || $is_revision || !$is_valid_nonce ) {
return;
}
if( isset( $_POST[ '_children' ] ) ) {
update_post_meta( $post_id, '_children', array_map( 'sanitize_text_field', $_POST[ '_children' ] );
}
}
add_action( 'save_post', 'test_meta_save' );
我知道以上也不正确。
Here you have the same problem that in your last question but this time with get_post_meta()
where last argument should be false
. because you are reading/creating arrays
values and NOT strings
values.
在您的代码中:
$children = get_post_meta( $postid, '_children', true);
您需要删除 get_post_meta()
函数中的最后一个参数,因为默认值为 false
.
相反,您将拥有:
$children = get_post_meta( $postid, '_children');
$arrlength = count($children);
for($x = 0; $x < $arrlength; $x++)
{
echo '<input type="text" name="_children[][firstname]" id="_children[][firstname]" value="' . $meta_values['_children'][0][$x][firstname] . '" /><br />';
echo '<input type="text" name="_children[][middlename]" id="_children[][middlename]" value="' . $meta_values['children'][0][$x][middlename] . '" /><br />';
}
参考文献:
我有一个对象集合需要 saved/retrieved 在 wp_postmeta table 中属于自定义 post 类型。
示例结构:
array(
array(
'firstname' => 'Johnny',
'middlename' => 'William'
),
array(
'firstname' => 'Jane',
'middlename' => 'Alice'
)
)
我希望能够像这样遍历对象:
$children = get_post_meta( $postid, '_children', true);
$arrlength = count($children);
for($x = 0; $x < $arrlength; $x++)
{
echo '<input type="text" name="_children[][firstname]" id="_children[][firstname]" value="' . $meta_values['_children'][0][$x][firstname] . '" /><br />';
echo '<input type="text" name="_children[][middlename]" id="_children[][middlename]" value="' . $meta_values['children'][0][$x][middlename] . '" /><br />';
}
我认为以上说法不正确。我正在尝试通过以下方式获取 posted 数据保存在 save_post
操作中:
function test_meta_save( $post_id ) {
// Checks save status
$is_autosave = wp_is_post_autosave( $post_id );
$is_revision = wp_is_post_revision( $post_id );
$is_valid_nonce = ( isset( $_POST[ '_children_nonce' ] ) && wp_verify_nonce( $_POST[ '_children_nonce' ], basename( __FILE__ ) ) ) ? 'true' : 'false';
// Exits script depending on save status
if ( $is_autosave || $is_revision || !$is_valid_nonce ) {
return;
}
if( isset( $_POST[ '_children' ] ) ) {
update_post_meta( $post_id, '_children', array_map( 'sanitize_text_field', $_POST[ '_children' ] );
}
}
add_action( 'save_post', 'test_meta_save' );
我知道以上也不正确。
Here you have the same problem that in your last question but this time with
get_post_meta()
where last argument should befalse
. because you are reading/creatingarrays
values and NOTstrings
values.
在您的代码中:
$children = get_post_meta( $postid, '_children', true);
您需要删除 get_post_meta()
函数中的最后一个参数,因为默认值为 false
.
相反,您将拥有:
$children = get_post_meta( $postid, '_children');
$arrlength = count($children);
for($x = 0; $x < $arrlength; $x++)
{
echo '<input type="text" name="_children[][firstname]" id="_children[][firstname]" value="' . $meta_values['_children'][0][$x][firstname] . '" /><br />';
echo '<input type="text" name="_children[][middlename]" id="_children[][middlename]" value="' . $meta_values['children'][0][$x][middlename] . '" /><br />';
}
参考文献: