Related content with content adapters

Hi everyone,
Since we have content adapters, I’m trying to migrate all contents into json data. But I’m still confused about the tags and keywords system under this new feature. How should I configure relarted contents? This is how I use a content adapter to create pages:

{{ $data := .Site.Data.blog.posts | jsonify | transform.Unmarshal }}

{{ range $data }}

    {{ $params := dict
       "author" .author
       "date" .date
       "tags" .tags
       "categories" .category
    }}
    {{ $content := dict
       "mediaType" "text/markdown"
       "value" .content
    }}
    {{ $page := dict
       "content" $content
       "kind" "page"
       "path" .title
       "title" .title
       "params" $params
    }}
    {{ $.AddPage $page }}
...

And here’s how I try to create related links:

    <h4>See Also:</h4>
    {{ $opt := dict
        "indices" "tags"
        "document" .
        "namedSlices" (slice (keyVals "tags" "Docker"))
    }}
    {{ $related := .Site.RegularPages.Related $opt | first 3 }}
    {{ with $related }}
    <ul>
        {{ range . }}
        <li><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></li>
        {{ end }}
    </ul>
    {{ end }}

It seems Hugo cannot doesn’t recognize the param ‘tags’, because I got this error:
executing "main" at <.Site.RegularPages.Related>: error calling Related: index "tags" not found
Is there any clue how this happened?

I’d like to see your entire site configuration and a sample of your data file (one or two records).

It’s quite simple.

languageCode = 'en-us'
title = 'Blog Template'
sectionPagesMenu = 'main'
summaryLength = 1

[taxonomies]
  category = 'categories'

[module]
  [[module.mounts]]
    source = "node_modules/bootstrap/scss"
    target = "assets/scss/bootstrap/"
  [[module.mounts]]
    source = "node_modules/bootstrap/dist/js/bootstrap.bundle.min.js"
    target = "assets/js/bootstrap.bundle.min.js"

  [[module.mounts]]
    source = "assets/scss"
    target = "assets/scss"
  [[module.mounts]]
    source = "assets/images"
    target = "assets/images"
{
"posts":[
{
    "title": "Introduction to Machine Learning",
    "author": "Bob",
    "tags": ["Machine Learning", "AI", "Data Science"],
    "date": "2023-11-20",
    "category": "AI",
    "content": "Machine learning is one of the core technologies of artificial intelligence, widely used in image recognition, natural language processing, and other fields. This article will introduce the basic concepts and common algorithms of machine learning.\n\n## Main Content\n- **Supervised Learning**: Such as linear regression and classification algorithms.\n- **Unsupervised Learning**: Such as clustering and dimensionality reduction.\n- **Model Evaluation**: Using metrics like accuracy and recall.\n\n```python\n# Example code\nfrom sklearn.linear_model import LinearRegression\nmodel = LinearRegression()\nmodel.fit(X_train, y_train)\n```"
}
]
}

There may be other problems, but it seems like tags should be a taxonomy.

Yes, you’re right. I just realized it. Thank you very much!

Your page map needs some adjustments:

  • Simply $data initialization
  • Handle dates correctly
  • Categories is a slice, not a scalar
content/posts/_content.gotmpl
{{ range site.Data.blog.posts }}
  {{ $dates := dict
    "date" (time.AsTime .date)
  }}
  {{ $params := dict
    "author" .author
    "tags" .tags
    "categories" (slice .category)
  }}
  {{ $content := dict
    "mediaType" "text/markdown"
    "value" .content
  }}
  {{ $page := dict
    "content" $content
    "dates" $dates
    "kind" "page"
    "path" .title
    "title" .title
    "params" $params
  }}
  {{ $.AddPage $page }}
{{ end }}

1 Like

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