英文:
How to limit query to 3 most recent posts with sanity?
问题
"latestArticles": *[_type == "article" && wasDeleted != true && isDraft != true] | order(publishDate desc){
title,
_createdAt,
},
英文:
I am trying to query the 3 most recent article posts but not sure how to set the limit
"latestArticles": *[_type == "article" && wasDeleted != true && isDraft != true] | order(publishDate desc){
title,
_createdAt,
},
答案1
得分: 1
你可以通过在查询末尾添加带有开始和结束索引的数组来切片查询,所以在你的情况下,它会看起来像这样:
"latestArticles": *[_type == "article" && wasDeleted != true && isDraft != true] | order(publishDate desc){
title,
_createdAt
}[0...2] // 获取条目 0、1 和 2(总共 3 个)
有一个专门介绍这个的部分在 sanity [文档][1]
[1]: https://www.sanity.io/docs/how-queries-work#16b34b599b2f
英文:
You can slice your query by adding array with start and end index at the end of query, so it would look like this in your case:
"latestArticles": *[_type == "article" && wasDeleted != true && isDraft != true] | order(publishDate desc){
title,
_createdAt
}[0...2] // get entries 0, 1, and 2 (3 total)
There is section dedicated to this in sanity documentation
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论