HUGO

How to list articles whose titles contain specific keywords

Hi everyone this is noob HUGO user XD
as the title. this is ex:
my content have four title articles:

hello world this is new year
how to something
thanks my frnd
how to be a sport man

now I want setting a keyword just like

“how to”

then how to use list or terms templates to generat html with list 2 articles above:

how to something
how to be a sport man

Im tried use relate. but it looks like only match full string.
need help thanks for everyone

On my last project I used shortcut that list articles that contain specific tag.

in Markdown I am using

{{< faq "Installation" >}}

and shortcode is

<ul class="faq-list">
  {{ $tagged := .Get 0 }}
  {{ range .Site.Pages }}
    {{ if in .Params.categories "FAQ" }}
      {{ if in .Params.tags $tagged }}
       <li><a href="{{ .Permalink }}">{{ .Title }}</a></li>
       {{ end }}
     {{ end }}
  {{ end }}
</ul>

Picking part of the word from title may be a little trickier, maybe just simplify that and use specific tag or category?

for your solution have a look on that: in | Hugo

{{ if in "this string contains a substring" "substring" }}Substring found!{{ end }}

Possible (not tested)

{{ $titlelo := lower .Title }}
{{ if in $titlelo "how to" }}
...
{{ end }}

config.toml

[taxonomies]
keyword = "keywords"

front matter:

+++
title = 'Article 1'
date = 2021-11-20T06:42:47-08:00
draft = false
keywords = ['how to','foo']
+++

With the configuration/content above, the list of articles with the “how to” keyword is published to:
https://example.org/keywords/how-to/

To display a list elsewhere:

{{ range where site.RegularPages "Params.keywords" "intersect" (slice "how to") }}
  <h2><a href="{{ .RelPermalink }}">{{ .Title }}</a></h2>
{{ end }}

To display a list with the keywords “how to” OR “beer”

{{ range where site.RegularPages "Params.keywords" "intersect" (slice "how to" "beer") }}
  <h2><a href="{{ .RelPermalink }}">{{ .Title }}</a></h2>
{{ end }}

To display a list with the keywords “how to” AND “beer”

{{ $p1 :=  where .Pages "Params.keywords" "intersect" (slice "how to") }}
{{ $p2 :=  where .Pages "Params.keywords" "intersect" (slice "beer") }}
{{ range (intersect $p1 $p2) }}
  <h2><a href="{{ .RelPermalink }}">{{ .Title }}</a></h2>
{{ end }}
1 Like

thanks I’m try to use. It’s work! can find special words in title.
but when I try to use paginate. I don’t know how to connect in .Title “how to” with .Paginate
I tried

{{- $page := (.Paginate (where (where .Site.RegularPages "Section" "post") "Params.title" "intersect" (slice "how to")) 14 ) -}}

and

{{- $page := (.Paginate (where (where .Site.RegularPages "Section" "post") "in" "Params.title" (slice "how to")) 14 ) -}}

there doesn’t work (when I use .Paginate because I’m not understand .Paginate :frowning: )

thanks for help. when I cant use in .Title "how to"

I’m try your plan and it’s work with paginate!

because my project need use more where to find article. the final code is

{{- $page := (.Paginate (where (where .Site.RegularPages "Section" "post") "Params.keywords" "intersect" (slice "how to")) 14 ) -}}
{{- range $paginator.Pages -}}
{{- Title -}}
{{- end -}}

It’s work. but still need use keywords in frontmatter (.md)
if can use title for match will very bingo :stuck_out_tongue:

{{ $p := slice }}
{{ range where .Site.RegularPages "Section" "post" }}
  {{ if in (lower .Title) "how to" }}
    {{ $p = $p | append . }}
  {{ end }}
{{ end }}

{{ range (.Paginate $p 14).Pages }}
  <h2><a href="{{ .RelPermalink }}">{{ .Title }}</a></h2>
{{ end }}

Great! it’s very help for me a noob user. thanks very much love u

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.