我有一个需要在自定义帖子类型的wp_postmeta表中保存/检索的对象的集合。使用WordPress发布元数据功能的HTML表单中的关联数组
结构示例:
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 />';
}
我不认为以上是正确的。我试图让与保存在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');
我知道上面是不正确的要么。
PL轻松尝试'$ meta_values ['_ children'] [$ x] [firstname]'只需删除'[0]'! –
@IsmailRBOUH我想知道那个[0]。数据库显示的值为NULL,所以我认为在我发布的'post_save'代码行中存在问题。 – rwkiii
'$ _POST ['_children']'是一个数组吗?你如何输入初始值! –