如何在 grav cms 中迭代主博客页面中的所有标签



我在user/config/site.yaml文件中定义了以下内容。

title: Grav
author:
  name: Joe Bloggs
  email: 'joe@test.com'
metadata:
    description: 'Grav is an easy to use, yet powerful, open source flat-file CMS'
taxonomies: [tag,author]

如您所见,我将以下内容定义为tamonomies:

taxonomies: [tag,author]

现在在我的个人博客文章中,我在标题中定义了以下内容:

---
title: 'My Blog post one'
visible: true
summery: 'Pellentesque ornare mi nec elementum fringilla. Nam aliquam urna metus, vel convallis leo vulputate ut. Sed ac tempor turpis, ut pellentesque nisi. Nulla in dui sit amet augue iaculis euismod ac a massa. Sed sollicitudin nibh nisi, eu efficitur ligula vulputate a.'
header_image_file : css-thumb.jpg
taxonomy:
    tag: [JS, jQuery , CSS]
    author: papita
---

如您所见,我定义了以下标签:

tag: [JS, jQuery , CSS]

问题::-

我想做的是在我的主博客页面的侧边栏中,并在列表中输出所有标签,如下所示::-

<ul>
  <li><a href="">{{ tag name }}</a><span>{{  number of articles that are tagged with this tag name }}</span></li>
  <li><a href="">{{ tag name }}</a><span>{{  number of articles that are tagged with this tag name }}</span></li>
  <li><a href="">{{ tag name }}</a><span>{{  number of articles that are tagged with this tag name }}</span></li>
</ul>

现在我尝试了以下 2 种让我接近的方法:

<ul> 
    {% for post in page.collection() %}
        <li><a href={{ post.url }}> {{ post.title }} </a></li>
    {% endfor %}
</ul> 

这当然不会给我标签名称,而只是给我每个博客个人博客文章。我还尝试了以下方法:

<ul> 
    {% for tag in config.site.taxonomies %}
        <li><a href="#"> {{ tag }} </a></li>
    {% endfor %}
</ul>

但这似乎也没有多大好处,我还能尝试什么才能得到想要的结果? 如何遍历所有标签,并显示每个标签发布的文章数量?

如果您在页面.md文件的标题中定义标记,则可以使用以下代码片段轻松遍历它们:

{% for tag in page.taxonomy.tag %} <li><a href="#"> {{ tag }} </a></li> {% endfor %}

有关更多信息,请查看官方文档或 grav 默认主题Quark

https://learn.getgrav.org/content/taxonomy

https://github.com/getgrav/grav-theme-quark/blob/develop/templates/partials/blog/taxonomy.html.twig

一般来说,我建议您查看官方 grav 页面上针对此类问题的默认主题和演示。通常,您会找到适合您需求的示例。

最新更新