Exclude page from homepage or list page

I have a sidebar which shows posts randomly with shuffle. For the homepage and every (paginated) list page, I would like to avoid showing a page in the sidebar if it appears in in the home page or list page. Can this be done?

You’re probably looking for something like .IsHome, which will return true in that case.

Not what I am looking for.

Right. I didn’t understand the first time.

If you want to filter an array, try the where function.

You don’t get my question.

Hmmm… What I understood was that you wanted to filter an array containing random posts, so it doesn’t show posts already present in the current page (which is a home or list page).

{{/* Paginated list. */}}
{{ $p := site.RegularPages }}
{{ range (.Paginate $p).Pages }}
  <h2><a href="{{ .RelPermalink }}">{{ .Title }}</a></h2>
{{ end }}
{{ template "_internal/pagination.html" . }}

{{/* Sidebar */}}
{{ $p := $p | complement .Paginator.Pages | shuffle | first 5 }}
{{ range $p }}
  <h3><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></h3>
{{ end }}

You must paginate before calling the side bar.

2 Likes

The sidebar is a partial. Is there a workaround for that?

list page, home page

{{/* Paginated list. */}}
{{ $p := site.RegularPages }}
{{ range (.Paginate $p).Pages }}
  <h2><a href="{{ .RelPermalink }}">{{ .Title }}</a></h2>
{{ end }}
{{ template "_internal/pagination.html" . }}

{{/* Call sidebar */}}
{{ $p := $p | complement .Paginator.Pages | shuffle | first 5 }}
{{ partial "sidebar.html" $p }}

layouts/partials/sidebar.html

{{ range . }}
  <h3><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></h3>
{{ end }}

I went with this solution to simplify things in my templates. Thanks @jmooring !

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