如何订购

How to order pieces

我在显示片段内容时遇到问题。

我希望作品按创作日期显示在 "apostrophe-pieces-pages" 中。我已经尝试 mongoDB 查询通过 "BeforeSend" 模块按 "createdAt" 字段对它们进行排序,但是这个结果出现在所有页面上,我不想要那个结果。

有什么方法可以按创建日期排序,只影响作品的页面吗?

apostrophe-pieces 有一个顶级选项,用于向您的索引添加一个简单的排序过滤器。在你的 pieces 子类中(你的模块扩展 apostrophe-pieces,而不是 apostrophe-pieces-pages)你应该能够添加一个 sort 过滤器到你的模块选项中以进行排序。

module.exports = {
  label: 'My Piece',
  extend: 'apostrophe-pieces',
  sort: { createdAt: -1 } // reverse chronological order
  // ... other configuration
};

更多关于 sorthttps://docs.apostrophecms.org/apostrophe/modules/apostrophe-docs/server-apostrophe-cursor#sort-value

更多关于 MongoDB sort 方法及其所用的内容 https://docs.mongodb.com/manual/reference/method/cursor.sort/

如果您想创建完全自定义的顺序,apostrophe-pieces-orderings-bundle 可能会有所帮助 https://github.com/apostrophecms/apostrophe-pieces-orderings-bundle

在撇号 3 中,可以通过以下方式设置顺序:

module.exports = {
  extend: '@apostrophecms/piece-type',
  options: {
    sort: {
      publishDate: -1
    }
  },
  ...
}