我正在将 Gatsby.js 与 WP API 结合使用。通常,您可以使用 GraphiQL 来处理查询,自动完成功能将有助于回答此类问题。但我认为要么该实用程序没有考虑这种初始级别,要么我没有正确使用 graphQL...
query workInnerPageQuery{
wordpressPage(id: { eq: "e52ad032-9468-5826-9320-a22da5b1e0c9" }) {
id
title
acf{
case_studies{ // this is a relation field
post_title
post_name
--- how do you get an ACF field here?
}
}
}
}
例如,我有一个自定义字段“副标题”。这不起作用...
acf{
case_studies{
post_title
post_name
acf{
subtitle
}
}
}
看到副标题是帖子类型“case_study”的 acf 字段,它是该特定页面上的关系项...如何访问 ACF 字段中的 ACF 字段?
还有其他人必须解决这个问题吗?
最佳答案
事实证明我的谷歌技能很糟糕。这可以通过插件实现: https://github.com/airesvsg/acf-to-rest-api-recursive
在您的 WordPress 网站上安装该插件,然后将其添加到您的函数文件中:
add_filter('acf/rest_api/recursive/types', function( $types ) {
$types += array(
'case_studies' => 'case_studies'
);
return $types;
} );
请记住,case_studies 是我的帖子类型。显然您需要将您的添加到数组中。
然后你就可以按照预期在graphql中递归访问acf...
acf{
case_studies{
post_title
post_name
acf{
subtitle
}
}
}
我会删除这篇文章,但我认为将其列在堆栈溢出上很有用。
关于wordpress - 如何在 GraphQL 中访问 ACF 关系字段中的自定义字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53659981/