How to use shuffle on my partiall

hello im trying to generate random posts/content in my sidebar widget, i made some research an i found that i have to use shuffle to do that, but i don’t know how to make it work can someone please help me by editing the code below, i tried doing it by my self but every time i push the changes if fails to build on netlify thank you in advance

{{- $recent := where .Site.RegularPages "Section" "in" .Site.Params.mainSections }}
{{- $recent_num := (.Site.Params.widgets.recent_num | default 10) }}

{{- if $recent }}
<div class="widget-recent widget">
	<h4 class="widget__title">{{ T "recent_title" }}</h4>
	<div class="widget__content">
		<ul class="widget__list">
			{{- range first $recent_num $recent }}
			<li class="widget__item">
				<a class="widget__link widget-recent__item" href="{{ .RelPermalink }}">
				{{- if .Params.thumbnail }}
					<img class="widget-recent__thumbnail" src="{{ .Params.thumbnail }}"/>
					{{ .Title }}
				{{- else }}
					- {{ .Title }}
				{{- end }}
				</a>
			</li>
			{{- end }}
		</ul>
	</div>
</div>
{{- end }}

hello,

this what i use for showing 6 random quotes from press reviews with quotes, when I build the site.
Data from /data/fr/press.yml

       {{ $datafr := index site.Data "fr" }}
        {{ with $datafr.press }}
            {{ if .enable }}
            <!-- Affiche les revues de presse -->
            <div class="row">
                {{ range first 6 (where .item ".quote" "ne" nil) | shuffle }}
                    <div class="col-md-4 col-sm-6">
                        {{ with .web }}<h4>{{.}}</h4>{{ end }}
                        {{ with .quote }}<p>«{{.}}»</p>{{ end }}
                    </div>
                {{ end }}
            </div>
        {{ end }}
        {{ end }}
2 Likes

Hi @SamHamou

This line of code fetches your content / posts and gets a default of 10 posts
{{- range first $recent_num $recent }}

What @divinerites example … would mean you just need to add shuffle to the query like this :

you might need to try this - to get much older posts than the first 10

{{- range $recent_num $recent | shuffle }}

good luck

1 Like

when i used range first and added shuffle in the end it worked but it only randomized the first posts and didn’t show the oldest one, and when i removed first i got this error 12:48:58 PM: Building sites … ERROR 2020/09/05 12:48:58 render of “page” failed: execute of template failed: template: _default/single.html:43:6: executing “_default/single.html” at <partial "sidebar.htm…>: error calling partial: “/opt/build/repo/themes/Feed/layouts/partials/sidebar.html:9:13”: execute of template failed: template: partials/sidebar.html:16:6: executing “partials/sidebar.html” at <partial $p $root>: error calling partial: “/opt/build/repo/themes/Feed/layouts/partials/widgets/recent.html:9:13”: execute of template failed: template: partials/widgets/recent.html:9:13: executing “partials/widgets/recent.html” at <$recent_num>: can’t give argument to non-function $recent_num
ERROR 2020/09/05 12:48:58 render of “page” failed: execute of template failed: template: _default/single.html:43:6: executing “_default/single.html” at <partial "sidebar.htm…>: error calling partial: “/opt/build/repo/themes/Feed/layouts/partials/sidebar.html:9:13”: execute of template failed: template: partials/sidebar.html:16:6: executing “partials/sidebar.html” at <partial $p $root>: error calling partial: “/opt/build/repo/themes/Feed/layouts/partials/widgets/recent.html:9:13”: execute of template failed: template: partials/widgets/recent.html:9:13: executing “partials/widgets/recent.html” at <$recent_num>: can’t give argument to non-function $recent_num
ERROR 2020/09/05 12:48:58 render of “page” failed: execute of template failed: template: _default/single.html:43:6: executing “_default/single.html” at <partial "sidebar.htm…>: error calling partial: “/opt/build/repo/themes/Feed/layouts/partials/sidebar.html:9:13”: execute of template failed: template: partials/sidebar.html:16:6: executing “partials/sidebar.html” at <partial $p $root>: error calling partial: “/opt/build/repo/themes/Feed/layouts/partials/widgets/recent.html:9:13”: execute of template failed: template: partials/widgets/recent.html:9:13: executing “partials/widgets/recent.html” at <$recent_num>: can’t give argument to non-function $recent_num
ERROR 2020/09/05 12:48:58 render of “page” failed: execute of template failed: template: _default/single.html:43:6: executing “_default/single.html” at <partial "sidebar.htm…>: error calling partial: “/opt/build/repo/themes/Feed/layouts/partials/sidebar.html:9:13”: execute of template failed: template: partials/sidebar.html:16:6: executing “partials/sidebar.html” at <partial $p $root>: error calling partial: “/opt/build/repo/themes/Feed/layouts/partials/widgets/recent.html:9:13”: execute of template failed: template: partials/widgets/recent.html:9:13: executing “partials/widgets/recent.html” at <$recent_num>: can’t give argument to non-function $recent_num
Total in 188 ms
12:48:58 PM: Error: Error building site: failed to render pages: render of “page” failed: execute of template failed: template: _default/single.html:43:6: executing “_default/single.html” at <partial "sidebar.htm…>: error calling partial: “/opt/build/repo/themes/Feed/layouts/partials/sidebar.html:9:13”: execute of template failed: template: partials/sidebar.html:16:6: executing “partials/sidebar.html” at <partial $p $root>: error calling partial: “/opt/build/repo/themes/Feed/layouts/partials/widgets/recent.html:9:13”: execute of template failed: template: partials/widgets/recent.html:9:13: executing “partials/widgets/recent.html” at <$recent_num>: can’t give argument to non-function $recent_num

@SamHamou try this …

<!-- Widget Random -->
{{ $filteredPosts := where .Site.RegularPages "Type" "in" site.Params.mainSections }}          
{{ $relatedPosts := shuffle $filteredPosts | first 3 }}  
          

{{- if $recent }}
<div class="widget-recent widget">
	<h4 class="widget__title">{{ T "recent_title" }}</h4>
	<div class="widget__content">
		<ul class="widget__list">
			{{ range $relatedPosts }}  
			<li class="widget__item">
				<a class="widget__link widget-recent__item" href="{{ .RelPermalink }}">
				{{- if .Params.thumbnail }}
					<img class="widget-recent__thumbnail" src="{{ .Params.thumbnail }}"/>
					{{ .Title }}
				{{- else }}
					- {{ .Title }}
				{{- end }}
				</a>
			</li>
			{{- end }}
		</ul>
	</div>
</div>
{{- end }}
<!-- Widget Random -->

with the code above i got this error

1:17:07 PM: Error: "/opt/build/repo/themes/Feed/layouts/partials/widgets/recent.html:6:1": parse failed: template: partials/widgets/recent.html:6: undefined variable "$recent"

My Bad … sorry I was trying to match my code to your CSS / Styles

change out this line …

{{- if $recent }}

so it looks like this …

{{- if $relatedPosts }}

1 Like

@SamHamou just remember hugo is a static site generator so its not going to be really RANDOM

Once the page is generated - the content is FIXED

1 Like

That was already explained to him previously: How to show a list of random posts.

thank you very much it’s working now :smile:

@sephore - yes I was just scanning through all their posts and see there is a common thread

@SamHamou - to help the community, try to explain what you have read on Hugo Docs or searched here on discourse

… and if you find a solution yourself – share it in Tips

1 Like

this worked for me How to use shuffle on my partiall - #5 by damien1