4 posts from page section

Please tell me how in the single.html template I can get the first four posts from the section of the current page without this page, as well as how I can get four random posts from the current section without the current page.

content
--tech
   --_index.md
   --2024                                      <- year
             /05                               <- month
                 /06/name1/index.md            <- post1
                 /07/name2/index.md            <- post2
                 /08/name3/index.md            <- post3
                 /09/name4/index.md            <- post4
                 /10/name5/index.md            <- post5
/*/name*/index.md
+++
title = "Title"
date = 2024-05-23T20:42:31+03:00
+++
tech/_index.md
+++
title = 'Tech'
date = 2023-01-01T08:30:00-07:00
draft = false
+++

I’m on post2.md single.html
I need posts 1,3,4,5.
Also, how do I get random 4 posts from tech in two months?

Get the section of the current page, then range over the section’s pages, ignoring the current one.
See Methods/Page in the documentation.

I looked at the documentation and did not understand how to do it. Can somebody help with code?
{{ range (.Paginate ( first 4 .Pages.ByTitle )).Pages }} Doesn’t work with page

execute of template failed at <.Paginate>: 
error calling Paginate: pagination not 
supported for this page: kind: "page",

Pagination is something different see docs

To range over pages use range .Pages or range .Regularpages on the section

For random pages see collections.shuffle

Have you seen that one:

.Pages or range .Regularpages doesn’t work for me.

This works for me

{{ with ($.Site.GetPage (printf "/%s" .Section)) }}
    {{ range first 4 .Pages }}
       {{ .Title }}
    {{ end }}
{{ end }}

But how do you get 4 pages without the current page. And how do you get randomness 4 without the current page? I saw Shuffle for random array, but how to remove current page from array?

{{ $p := where .CurrentSection.RegularPages "Path" "ne" .Path }}

<h2>First four in page collection</h2>
{{ range $p | first 4 }}
  <h3><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></h3>
{{ end }}

<h2>First four random</h2>
{{ range $p | shuffle | first 4 }}
  <h3><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></h3>
{{ end }}
2 Likes

Thank you so much. Everything is working perfectly. You’ve been a great help to me.

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