在右侧的类别下添加新的 metabox(post 类型)
Add new metabox on right side under the categories (post type)
当您在仪表板中点击添加新 post 时,您可以在左侧看到标题描述,在另一侧看到类别和其他选项。我必须再添加两个小部件,第一个就像我设法使用 "multiple post thumbnail" 添加的特色图片,另一个小部件用户可以在其中添加 url。我可以得到任何人的帮助吗?
这涉及在 register_post_type
function and then doing the saving procedures using the save_post
(或类似的)WordPress 挂钩中为 post 类型注册元数据框。在 metaboxes 中创建 UI,然后在 save_post
中您可以读取 $_POST
.
中包含的值
假设元数据框包含一个字段,例如
<input type="text" name="my-text-field">
然后你可以在save_post
中将值保存为post元
add_action('save_post', function ($postId, $post, $update) {
$textValue = $_POST['my-text-field'];
update_post_meta($postId, 'text_value', $textValue);
}, 25, 3);
我跳过了 post 类型和数据验证以保持示例简单。您应该始终检查数据是否有效,如果不需要或被认为有害则不保存。
metabox 字段应始终包含在 $_POST
中,方法是使用常规 HTML 输入。如果您需要画廊和颜色选择器之类的东西,您可以将这些输入包装到 JS UI 中。
当您在仪表板中点击添加新 post 时,您可以在左侧看到标题描述,在另一侧看到类别和其他选项。我必须再添加两个小部件,第一个就像我设法使用 "multiple post thumbnail" 添加的特色图片,另一个小部件用户可以在其中添加 url。我可以得到任何人的帮助吗?
这涉及在 register_post_type
function and then doing the saving procedures using the save_post
(或类似的)WordPress 挂钩中为 post 类型注册元数据框。在 metaboxes 中创建 UI,然后在 save_post
中您可以读取 $_POST
.
假设元数据框包含一个字段,例如
<input type="text" name="my-text-field">
然后你可以在save_post
add_action('save_post', function ($postId, $post, $update) {
$textValue = $_POST['my-text-field'];
update_post_meta($postId, 'text_value', $textValue);
}, 25, 3);
我跳过了 post 类型和数据验证以保持示例简单。您应该始终检查数据是否有效,如果不需要或被认为有害则不保存。
metabox 字段应始终包含在 $_POST
中,方法是使用常规 HTML 输入。如果您需要画廊和颜色选择器之类的东西,您可以将这些输入包装到 JS UI 中。