如何使用特定参数 "true" 仅显示来自特定类别的 1 post
How to display only 1 post from certain category with certain params "true"
这个想法是如何只显示来自某个类别的 1 个最新 post,如果它具有参数 "true"
{{ range (where .Data.Pages "Type" "blog") 1 }}
{{ if and (in .Params.categories "photography") (in .Params.featured "true") }}
上面的代码可以正常工作,但它会呈现类别为 foo 的所有 post(超过 1 个 post)
{{ $photography := .Data.Pages.ByParam "categories" }}
{{ if and (in .Params.featured "true") (eq .Params.categories "photography") }}
{{ range first 1 $photography }}
上面的代码没有错误,但根本没有呈现
有线索吗?
有一种方法可以根据 categories
的定义来解决这个问题。
解决方案(作为分类法的类别)
在这种情况下,我们会说它是分类法,因为如果您未在站点配置中指定任何分类法,categories
就是默认分类法。
我们做的第一件事是获取类别值为 photography
的页面集合
{{ $photography := .Site.Taxonomies.categories.photography }}
然后我们必须找到一种方法来使用函数 GroupByParam
并按 featured
分组来获取具有特色 true
的页面
{{ range ($photography.Pages.GroupByParam "featured") }}
{{ end }}
此范围内的组将 return .Params.featured
的 .Key
值,因此我们将检查每个值,直到它是 true
。找到特色组后,按您喜欢的顺序排序,然后 return 您想要的那 (1) 个。
{{ if eq .Key true }}
{{ range last 1 .Pages.ByDate }}
// The `.` context here is a page. Use it accordingly
{{ end }}
{{ end }}
完整代码示例(类别作为分类法)
{{ $photography := .Site.Taxonomies.categories.photography }}
{{ range ($photography.Pages.GroupByParam "featured") }}
{{ if eq .Key true }}
{{ range last 1 .Pages.ByDate }}
// The `.` context here is a page. Use it accordingly
{{ end }}
{{ end }}
{{ end }}
注释:
- 该解决方案使用
ByDate
进行排序,但是 can use any valid page collection sorting 相应地使用 first
或 last
来获取您需要的页面。
- 确保分组参数 (
featured
) 中的值是同一类型(在本例中为布尔值)。如果混合使用,它们将无法正常工作。如果您将它们设为字符串,它们将起作用,但需要检查字符串 "true"
.
- 如果您想使用分类法但不想为它们构建页面,您可以在配置文件中
disableKinds = ["taxonomy","taxonomyTerm"]
作为 per Hugo docs。
这个想法是如何只显示来自某个类别的 1 个最新 post,如果它具有参数 "true"
{{ range (where .Data.Pages "Type" "blog") 1 }}
{{ if and (in .Params.categories "photography") (in .Params.featured "true") }}
上面的代码可以正常工作,但它会呈现类别为 foo 的所有 post(超过 1 个 post)
{{ $photography := .Data.Pages.ByParam "categories" }}
{{ if and (in .Params.featured "true") (eq .Params.categories "photography") }}
{{ range first 1 $photography }}
上面的代码没有错误,但根本没有呈现
有线索吗?
有一种方法可以根据 categories
的定义来解决这个问题。
解决方案(作为分类法的类别)
在这种情况下,我们会说它是分类法,因为如果您未在站点配置中指定任何分类法,categories
就是默认分类法。
我们做的第一件事是获取类别值为 photography
{{ $photography := .Site.Taxonomies.categories.photography }}
然后我们必须找到一种方法来使用函数 GroupByParam
并按 featured
true
的页面
{{ range ($photography.Pages.GroupByParam "featured") }}
{{ end }}
此范围内的组将 return .Params.featured
的 .Key
值,因此我们将检查每个值,直到它是 true
。找到特色组后,按您喜欢的顺序排序,然后 return 您想要的那 (1) 个。
{{ if eq .Key true }}
{{ range last 1 .Pages.ByDate }}
// The `.` context here is a page. Use it accordingly
{{ end }}
{{ end }}
完整代码示例(类别作为分类法)
{{ $photography := .Site.Taxonomies.categories.photography }}
{{ range ($photography.Pages.GroupByParam "featured") }}
{{ if eq .Key true }}
{{ range last 1 .Pages.ByDate }}
// The `.` context here is a page. Use it accordingly
{{ end }}
{{ end }}
{{ end }}
注释:
- 该解决方案使用
ByDate
进行排序,但是 can use any valid page collection sorting 相应地使用first
或last
来获取您需要的页面。 - 确保分组参数 (
featured
) 中的值是同一类型(在本例中为布尔值)。如果混合使用,它们将无法正常工作。如果您将它们设为字符串,它们将起作用,但需要检查字符串"true"
. - 如果您想使用分类法但不想为它们构建页面,您可以在配置文件中
disableKinds = ["taxonomy","taxonomyTerm"]
作为 per Hugo docs。