从WordPress中的元框中获取信息并将其显示在页面模板中,可以通过以下步骤实现:
下面是一个示例代码,演示了如何从WordPress的元框中获取信息并将其显示在页面模板中:
// 在文章编辑页面添加自定义元框
function add_custom_meta_box() {
add_meta_box(
'custom_meta_box',
'自定义元框',
'render_custom_meta_box',
'post',
'normal',
'default'
);
}
add_action('add_meta_boxes', 'add_custom_meta_box');
// 渲染自定义元框
function render_custom_meta_box($post) {
// 获取存储在元框中的信息
$custom_info = get_post_meta($post->ID, 'custom_info', true);
// 显示一个输入框,用于编辑信息
echo '<label for="custom_info">自定义信息:</label>';
echo '<input type="text" id="custom_info" name="custom_info" value="' . esc_attr($custom_info) . '" />';
}
// 保存自定义元框中的信息
function save_custom_meta_box($post_id) {
if (array_key_exists('custom_info', $_POST)) {
update_post_meta(
$post_id,
'custom_info',
sanitize_text_field($_POST['custom_info'])
);
}
}
add_action('save_post', 'save_custom_meta_box');
// 在页面模板中显示自定义信息
$custom_info = get_post_meta(get_the_ID(), 'custom_info', true);
echo '自定义信息:' . $custom_info;
在上述示例中,我们首先使用add_meta_box函数创建了一个名为"custom_meta_box"的自定义元框,并将其关联到文章类型。然后,在元框的回调函数中,我们使用get_post_meta函数获取存储在元框中的信息,并在页面模板中使用echo语句将其输出。
请注意,上述示例中的"custom_info"是一个示例元框名称,你可以根据实际需求进行修改。另外,为了安全起见,我们使用了sanitize_text_field函数对用户输入进行了过滤和清理。
对于腾讯云相关产品和产品介绍链接地址,由于要求不能提及具体品牌商,建议你参考腾讯云的官方文档或咨询腾讯云的技术支持,以获取与WordPress集成的相关产品和服务信息。
领取专属 10元无门槛券
手把手带您无忧上云