HUGO

Exclude Post Type

How do I exclude a specific post type from the main posts loop. I’d like to have two sections on my website. One for posts, another for photos. However, I want to be able to type “hugo new post/test.md” and have it ONLY listed in the posts section, but be able to type “hugo new photo/test-2.md” and have it ONLY listed in the photos section. As of right now, I can create a new “photo” post, but both sets of posts are listed in the main feed.

This is the code I have so far, but to no avail.

{{ range .Paginator.Pages }}
{{ if ne .Type.Photo true}}
	{{ .Render "li" }}
{{ end }}

Thanks in advance.

You code above will not work because your if-statement isn’t correct. A page has an attribute .Type whose value might be "photo" as a string. .Type.Photo does not exist.

Furthermore, ne compares two objects of the same type, e.g. strings like "photo" and returns a boolean. Currently, you’re comparing the non-existing .Type.Photo with a boolean.

Your if-statement should look more as follows:

{{ if eq .Type "photo" }}
{{ end }}

However, filtering a list can be done more elegantly by using the where template func. As you can see it looks very similar to the if-statement above:

{{ range where .Site.Pages "Type" "eq" "photo" }}
{{ end }}

Thank you, digitalcraftsman. That’s exactly what I was looking for! I promise I attempted to do my due diligence and read through the Hugo docs, but is it just me, or are they fairly difficult to understand?

Thanks again.

There have been earlier discussion about the structure of the docs (just do a quick forum search). Most things are documented in detail but sometimes it’s hard for new users to get a 10.000 foot overview of the concepts behind Hugo.

If you’re using Hugo since a longer time you already now where to find most of the information so I might not notice such hurdles for newcomers. Users like you would be a great way to improve the docs by providing feedback.

The awesome @rdwatters already proposed a new organization for the docs. He’s writing technical documentations professionally and should a have good amount of experience from which we could benefit.

Feel free to leave your 2 cents.