wordpress - 在 WP Rest API 中获取 MetaBox 值
问题描述
我有一个注册的元框:
$meta_boxes[] = [
'title' => esc_html__( 'Background', 'background' ),
'id' => 'background',
'context' => 'normal',
'post_types' => array('post', 'page'),
'fields' => [
[
'type' => 'image_advanced',
'name' => esc_html__( 'Background', 'background' ),
'id' => 'background',
'max_file_uploads' => 1,
],
],
];
我正在尝试MetaBox
像这样获得该值:
$query = new WP_Query( $args );
$page = $query->post; // there is a post
$meta = get_post_meta($page->ID, 'background', true); // $page->ID is integer
return [
'uri' => $uri,
'ID' => $page->ID,
'page' => $query->post,
'meta' => $meta
];
,但$meta
是一个整数,如2225
。该帖子已填充此元框。
如果我尝试使用标准的 Rest API 端点,我会得到以下值:
.... post data
"meta_box": {
"background": [
{
"width": 150,
"height": 150,
"file": "2020/10/163593234-e1603902300928.jpg",
........
我的问题是如何通过带有自定义端点的 Rest API 获取发布 MetaBox 值,因为,get_post_meta
返回(我猜)一些关系?
解决方案
您必须使用wp_get_attachment_image_src:
$meta = get_post_meta( $page->ID, 'background', true ); // $page->ID is integer
if( $meta ) {
$img = wp_get_attachment_image_src( $meta );
}
你可以在这里wp_get_attachment_image_src
看到返回值。
推荐阅读
- python - Flask-Admin 与多对多关系中的附加字段
- java - 测试使用多个数据源的 JPA 应用程序
- path - F#:尝试在 VS Code 上运行简单的 F# 程序时出现路径错误
- matlab - 问题 在 windows 10 中安装 Manopt matlab 工具箱
- excel - 在索引匹配中嵌入 OR 语句不起作用
- java - wifi扫描是否需要BroadcastReceiver?
- algorithm - 证明算法的 Big-O 运行时
- python - 我知道这很原始,但我想让这个功能以更短的时间更有效地工作
- algorithm - 使用桶算法排序
- mysql - 如何将 UNION 与两个选择和一些 JOINS 一起使用