[SOLVED] How to use `range` to display partial on all pages?

I have a very simple partial to display my 10 latest blog posts:

{{ range first 10 .Data.Pages }} 
{{ if eq .Type "blog" }}
  <li>
      <a href="{{ .Permalink }}">
      {{ .Title }}

and the same for the status updates.

Works fine on the home page but only displays blog posts on the blog page and status updates on the status page. GitHub repository here.

Is there a golang template syntax reference so I can work out how to fix it? I’ve looked at golang Package template but it doesn’t make much sense to me. I know this is probably really simple but I’m frontend designer with not much programming experience!

Any pointers gratefully received (and also any recommendations for learning golang!)

many thanks, Jake

If it’s specific posts that you want to feature site wide with your partial you should use where in your range function. See: https://gohugo.io/functions/where/#readout

Otherwise it’s normal that you’re getting different posts rendered under different sections with what you posted above, since you’re only fetching the 10 latest posts and these are different on the homepage and from within other sections.

1 Like

just what I was looking for, thank you, trying to figure it out today :slightly_smiling_face:

That worked a treat. Just for the record, I’d copied and pasted the above code from Dan Bahrami’s excellent intro post for using Hugo with Gulp.

{{ range first 10 .Data.Pages }} 
{{ if eq .Type "blog" }}

The .Data.Pages variable is AFAIK only for use on the home page. I changed it to .Site.Pages and added where, wrapped it in parentheses to be able display first 10, so it looks like this:

{{ range first 10 (where .Site.Pages "Section" "blog") }}

Thanks for help @alexandros :slightly_smiling_face:

2 Likes

.Data.Pages are actually the children of the page in the context. So in your prior example, the partial listed all of the homepage children, and when in the Post section, only the post section children and so on…

2 Likes

Brilliant, thanks for clarification :slightly_smiling_face: