wordpress - 为wordpress rest api公开acf的所有字段
问题描述
我在 wordpress 网站中使用高级自定义字段。我创建了一个在帖子中使用的自定义字段。然后我为主页创建了一个自定义字段(关系字段),以便我可以从那里选择帖子。当我为我的主页进行 api 调用时,我看到了带来帖子的 acf,但我看不到帖子中使用的 acf。如何公开所有字段以将它们与我的 api 一起使用?它应该是这样的
{
acf: {
relationship_field: [
{
id: 5,
post_content: "something...",
...
acf: {
...
},
...
}
]
}
}
我看不到嵌套的 acf(我不知道实际上是否应该这样,但你明白了)。提前致谢。
解决方案
如果我理解正确,您应该调用register_rest_field
以便在获取帖子时将额外数据添加到响应中。
register_rest_field('post', '_embedded', array(
'get_callback' => function ($post) {
return [
'field_1' => get_field('field_xxxxxxxxxxxxx', $post['id']),
'field_2' => get_field('field_xxxxxxxxxxxxx', $post['id']),
];
}
));
推荐阅读
- java - 如何去掉不重要的节点?
- nextflow - 在 Nextflow 中获取两个输出文件时出现问题
- python - 如何使用 for 循环/范围来结束 Python 中的特定字符输入?
- blockchain - 智能合约没有产生结果,我如何查看两个结果或获得两个结果的总数
- javascript - TypeScript 找不到名称“SpeechGrammarList”,在“DOM”中实现
- c# - 我正在尝试用 C# 读取、排序然后写入
- bash - 如何使用netcat接收txt文件的内容
- python - 即使在类属性中满足要求时也会引发 AssertionError?
- multithreading - Rust 中的多线程下载时出现奇怪的错误
- regex - 如何根据键正则表达式查找值