You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在11ty中全局设置标签分类下的帖子按字母顺序排序

Solution for Global Alphabetical Sort + Tag Filtering in 11ty

Great news—you absolutely can achieve this with a global setup, no need for per-tag configuration! Your existing postsAscending collection is already handling the alphabetical sorting perfectly; we just need to add dynamic tag filtering in your tag template to only show relevant posts.

Option 1: Template-Level Filtering (Quickest Fix)

This approach uses Liquid's built-in logic to filter your sorted collection directly in the tag template, no extra config changes required.

Assuming your tag template has access to the current tag via page.tag (adjust this variable name if your setup uses something like tag or page.params.tag), update your loop to:

{% for post in collections.postsAscending %}
  {% if post.data.tags contains page.tag %}
    <!-- Your post rendering code here (e.g., title, excerpt, link) -->
    <h2><a href="{{ post.url }}">{{ post.data.title }}</a></h2>
  {% endif %}
{% endfor %}

This loops through your globally sorted posts and only renders those that include the current tag in their tags array.

Option 2: Custom Filter (Cleaner Template Code)

If you want to keep your template code more streamlined, add a custom filter to your 11ty config that filters posts by tag while preserving the sorted order:

In your .eleventy.js config file, add:

eleventyConfig.addFilter("filterByTag", (posts, targetTag) => {
  // Filter the sorted posts to only include those with the target tag
  return posts.filter(post => post.data.tags.includes(targetTag));
});

Then in your tag template, use the filter to get the sorted, tagged posts:

{% assign sortedTaggedPosts = collections.postsAscending | filterByTag: page.tag %}
{% for post in sortedTaggedPosts %}
  <!-- Your post rendering code here -->
  <h2><a href="{{ post.url }}">{{ post.data.title }}</a></h2>
{% endfor %}

Key Notes

  • Both approaches reuse your existing postsAscending collection, so you only maintain one global sort logic.
  • Ensure your posts have a tags array in their frontmatter (e.g., tags: ["photography", "nature"]) for the filter to work correctly.
  • If your tag variable in the template isn’t page.tag, replace it with the correct variable name from your 11ty tag setup (common alternatives are tag or page.data.tag).

This setup will give you alphabetically sorted posts across your entire site, and clicking a tag will only display posts that include that tag—all with a single global configuration!

内容的提问来源于stack exchange,提问作者Neo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.27 10:37:31