【发布时间】:2023-02-02 16:13:30
【问题描述】:
我有一个用 NextJS 构建的博客,由 Sanity 提供支持。我想开始使用标签/类别来标记帖子。
每个帖子可能有很多类别。
类别是帖子的参考:
defineField({
name: 'category',
title: 'Category',
type: 'array',
of: [
{
type: 'reference',
to: [
{
type: 'category',
},
],
},
],
}),
这是我的 GROQ 查询:
*[_type == "post" && count((category[]->slug.current)[@ in ['dogs']]) > 0] {
_id,
title,
date,
excerpt,
coverImage,
"slug": slug.current,
"author": author->{name, picture},
"categories": category[]-> {name, slug}
}
上面的代码是硬编码的,但是用 $slug 换出 'dogs' 会导致查询失败。 (其中 $slug 是提供的参数)
{
$slug: 'travel'
}
如何使上述动态?
【问题讨论】: