How add conditions in a shortcodes to list my post

I try to make a shortcode that list my posts
and I would like to classify the articles in 2 section

I did a themes\sandbox\layouts\shortcodes\blogposts.html

{{ $count := .Get "count" | default 6 }}
{{ $pages := where .Site.RegularPages "Type" "in" site.Params.mainSections }}

<section class="section">
    <div class="container">
        <h2 class="title has-text-centered">Blog</h2>
        <div class="columns is-multiline">
            {{ range first $count $pages }}
            <div class="column is-one-third">
                {{ partial "widgets/post-card.html" . }}
            {{ end }}

in my config.toml

    author = "Alex"
    authorImage = "/images/avatar-musk.jpg"
    mainSections = ["blog", "tutos"]

And when i put {{< blogposts >}} in my markdown file

But i would like get <h2>Blog</h2> and another section


I suppose that you need two where statements then, one selecting for blog and the other one for tutos. But that’s just a wild guess, as I do not really understand the last two lines of your post.


In your shortcode, you always put an H2 heading Blog

<h2 class="title has-text-centered">Blog</h2>

What you do is looping over the pages without sorting by Sections.
I would try :

  • to add an order by section in this line {{ $pages := where .Site.RegularPages "Type" "in" site.Params.mainSections }}
  • loop over the pages and watch the change of section in order to put the right H2 heading.

Sorry, I can’t give an example now.

1 Like