Related Content - Custom Index

I am trying to tweak the related content so that there are always some related showing.

My related partial:

{{ $related := .Site.RegularPages.Related . | first 5 }}
{{ with $related }}
{{ end }}

And in my config.toml:

[related]
  toLower = true
  threshold = 0
  [[related.indices]]
    name = "custom_title"
    weight = 30
  [[related.indices]]
    name = "categories"
    weight = 50

For some reason, I cannot get it to use the “custom_title” from the frontmatter of the posts. It completely ignores that and only uses the “categories”

What am I missing here?

The index name. This value maps directly to a page param.

I would assume from this part in the docs that custom page parameters should be available, never tried it though. Two ideas: try params.custom_title or some variation of that. And play around with the weight. Maybe the 30 weight is low enough to have more hits in the categories index. Set them to 50 and 50 and print 20 results. If still no custom title index item turns up it’s the custom params that are not available at that point :slight_smile:

That is not correct. What he already has should work (on the tech side of it).

If I do “categories,” they show just fine…

I cannot get this to work with “title” or “custom_title”. Both of these should work and show related items because the custom_title has text that is used throughout multiple pages.

Is the standard “title” from the front matter a valid index to use? Can you use any item from the frontmatter, including custom params?

@bep

I just ran some tests and it looks like the custom parameter needs to be a literal match.

I thought it would look at the content of the custom front matter parameter and find matches from within the string.

For example, consider two pages with “custom_title” in the front matter with the following:

  • Page A - A list in New York
  • Page B - A list in New York

I thought it would iterate over the “A list in New York” and find a match in the related.

That is not the case.

So is there anyway to back fill the related with other pages putting the related first?

Yes, but the types are currently limited to strings (e.g title), string slices (e.g. tags, keywords, categories) and dates. You should get an error for any unsupported type.

@bep - thanks for the responses. I understand this more clearly now.

I found a hacky way to have it backfill related content…

in config.toml:

[related]
  toLower = true
  threshold = 10
  [[related.indices]]
    name = "categories"
    weight = 90
  [[related.indices]]
    name = "related_hack"
    weight = 10

And on every section in the frontmatter:

related_hack: hack

I’m not sure I understand the hack, but if you could maybe have a look at the cascade keyword if you need to fill a certain value into many pages.

@bep again, many thanks for your responses.

So my hack won’t work the way I intended.

I am trying to find a way to have related pages show by category, then have a way for it to include other pages if there aren’t enough related pages on my limit.

I want to have the other non-related pages to be included after the related pages, but in a random manner so that I do not have the same non-related pages appearing throughout the site.

If there aren’t enough related categories, then using another page param results in the non-related pages being in alphabetical order - meaning the same pages get repeated throughout the site.

I understand this is an edge-case. Is this something I would need to use scratch for?

Okay, I figured out how to “backfill” related content if there aren’t enough matches.

{{ $related := where (.Site.RegularPages.Related . ) "Section" "section-name" }}
{{ $other :=  where .Site.RegularPages "Section" "section-name" | shuffle }}
{{ $six :=  append $other $related }}
     {{ range first 6 $six }}
          <!-- items -->
     {{ end  }}

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