1

我最近尝试过 sanity.io CMS 来管理我个人博客上的内容。但是,我很难在有关“自动引用”的文档中找到一部分(这只是我的术语)。我希望我的每篇博文都有下一篇和上一篇博文的数据,所以我可以在底部创建按钮来导航到下一篇或上一篇博文。我怎样才能实现它?谢谢

4

1 回答 1

2

对我来说,这是一项查询而不是参考的工作。

无法按照您描述的方式“自动引用”某些内容,但是有几种方法可以实现您的效果:

1.手动链接它们

2. 创建一个自定义输入字段,用于查询您的上一个/下一个帖子。更多关于自定义输入组件的信息:https ://www.sanity.io/docs/custom-input-widgets

3. 使用 GROQ 查询获取所有帖子并找到您需要的帖子(在 JS 中):

const posts = await Sanity.fetch(`*[_type == 'post']`);

const currentPostIndex = posts.findIndex(post => post.id === currentPost.id);
const previousPost = posts[currentPostIndex - 1];
const nextPost = posts[currentPostIndex + 1];

4.添加日期查询以获取具有GROQ的相邻帖子(在JS中):

const posts = await Sanity.fetch(`*[_type == 'post' && _id == '${currentPost.id}' ][0] {
  'currentPost': {
    ...
  },
  'previousPost': *[_type == 'post' && _createdAt < ^._createdAt][0],
  'nextPost': *[_type == 'post' && _createdAt > ^._createdAt] | order(_createdAt asc)[0]
}`);

const currentPostIndex = posts.findIndex(post => post.id === currentPost.id);
const previousPost = posts[currentPostIndex - 1];
const nextPost = posts[currentPostIndex + 1];

有关 GROQ 的更多信息https://www.sanity.io/docs/query-cheat-sheet

于 2020-07-07T17:43:47.107 回答