Category Name and It's Posts Help!

Hi, I am trying to show category name and it’s post but not able to get the posts from that category.

                        {{ range $name, $taxonomy := .Site.Taxonomies.categories }}
                        <div class="dropdown-posts hidden" id="{{$name}}">
                            <p>{{$name | title}}</p>
                            <div class="nav-custom-posts">
                                {{ range where $.Site.RegularPages "Params.categories" "intersect" (slice $name) }}
                                <a href="{{.Permalink}}" itemscope="" itemtype="https://schema.org/Guide" class="nav-blog">
                                    <p itemprop="name">
                                        {{.Title}}
                                    </p>
                                    <meta itemprop="thumbnailUrl"
                                        content="{{.Params.featured_Image}}" />
                                    <meta itemprop="description"
                                        content="{{.Params.meta_Description}}" />
                                    <meta itemprop="dateCreated" content="{{.Date}}" />
                                    <link itemprop="url"
                                        href="{{.Permalink}}" />
                                    <link itemprop="image"
                                        href="{{.Params.featured_Image}}" />
                                    <meta itemprop="author creator" content="{{.Params.author}}" /><span
                                        class="blog-post-time" itemprop="dateCreated">Last Updated on 
                                        {{ if .Params.updateDate }}
                                        {{dateFormat "2 Jan" .Params.updateDate}}
                                         {{ else }}
                                        {{dateFormat "2 Jan" .Date}}
                                        {{ end }}
                                    </span>
                                </a>
                                {{ end }}
                            </div>
                        </div>
                        {{ end }}

This is the code. Please help me show categorie’s posts

First, you do not need to use the where function to get a term’s weighted pages.

{{ range $term, $weightedPages := .Site.Taxonomies.categories }}
  <h2>{{ $term }}</h2>
  {{ range . }}
    <h3><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></h3>
  {{ end }}
{{ end }}

Second, your existing code is functional with my test data and with my content structure and with my layout structure. I have no idea what your site looks like.

You are more likely to receive a prompt and accurate response if you post a link to the public repository for your project.

See https://discourse.gohugo.io/t/requesting-help/9132.

Let us see your code

Include a link to the source code repository of your project, because we really need the context of seeing your templates and partials to be able to help you. It is trivial to do a quick git clone on your repo, then run hugo server in your project, to help you out. On the other hand, recreating your code from screenshots, or sort of guessing at it, is not.

If you can’t share your repository for whatever reason, consider creating a dummy repo that you can share, which reproduces the problem you’re experiencing.

Thanks That Worked.

Can you help me with a piece of code. that after 1st category it print hidden in class like

{{ range $term, $weightedPages := .Site.Taxonomies.categories }}
  <h2 class="{{ if after 1 . }}hidden{{end}}">{{ $term }}</h2>
  {{ range . }}
    <h3><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></h3>
  {{ end }}
{{ end }}

I tried to above one but that doesn’t work.

Thanks again!

Use a counter

{{ $i := 0 }}
{{ range $term, $weightedPages := .Site.Taxonomies.categories }}
  {{ if $i }}
    <h2 class="hidden">{{ $term }}</h2>
  {{ else }}
    <h2>{{ $term }}</h2>
  {{ end }}
  {{ $i = add $i 1 }}
  {{ range . }}
    <h3><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></h3>
  {{ end }}
{{ end }}

Thanks <3

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