Using Content Type to categorise list of posts

I have a need where I want to categorize the posts I create into different sections (for example, photo, quote, or a post etc). I do not want to do so as sections, but just want a single section for all posts. This is just to keep the management simple for every post that is published. However, I do want to show different views for the lists of these posts by their category. One option I thought I could use was Content Types.

I searched through the documentation and also the discussion forum. However, I could not find a way to do so. I also found this post. But it involves modifications to defaults (especially in single’s html to get a list) that I would like to avoid.

To summarize, I want a list of posts to be accessed by category, but each post being the same section type (same directory of content). Please advise how this can be achieved.

Hello @am1t,

you’re essentially describing Hugo’s taxonomy feature. Taxonomies allow you to group content based on something they’ve all in common, e.g. they all contain photos or quotes. A content file can of course be part of multiple taxonomies.

The location of the content file is irrelevant. This means that all posts can remain where they are which doesn’t change their content type either.

All you’ve to do is to define your taxonomies in the config file, e.g. category. Then you can add posts to categories in the front matter as such:

category = ["photo", "quote"]

The content file above has been added to the photo and quote category.

Thank you @digitalcraftsman for your response.

I am indeed using the taxonomy (currently tags) to categorize the posts. However, I have two main issues I would like to handle.

  • The way to access this is via /<taxonomy>/<term>. For example, I am using /tags/quotes to fetch all posts with quotes. Somehow I would have preferred just /quotes – the way sections are accessed.
  • To define the way the list of posts with said tags is rendered, I had to modify the default list.html and handle possible taxonomy terms in if ... else .. end block. I am not sure if this is the way, but I could find no other way.

My intention was to check if Type can be applied on customizing the posts without physically segregating them in separate folders. Please correct me if I am missing anything.

So, let me see if I understand. You want to put all your articles in one folder. And then have a /quotes page where you can list all your posts that are labeled as quotes?

If that’s so, this is how you do it:

  1. Declare a category taxonomy in your config.toml. More info here
[taxonomies]
  category = "categories"
  1. Create posts in your /posts folder and categorize and set the taxonomy:
title: Post #1
categories:
  - quotes
  1. Create your terms.html template. More info here. There you will list all your quotes. You don’t need to create the page, just the template.

You can find almost everything you need in the documentation for taxonomies and taxonomy templates. And I created a tutorial that may be helpful for you: Handling taxonomies like a boss - update

Thank you @guayom, this does look a lot closer. Not sure how did I not find these help artifacts.

Though I am not yet clear how I will work this out, but this gives me a good start. Let me try this. I will update the thread.

The main purpose of content types is to allow you customize the layout of a piece of content so it is different from the other pieces in that section.

Tumblr uses different types for say a photo, video or text. WordPress has built-in types called post formats.

They are for presentation changes, such as not showing the title element for a quote, or using a different font and size.

Content types are a type of taxonomy, in the literal sense, but will not create pages like a defined taxonomy like categories, which will create list pages.

@am1t you are asking for a lot of things in this one thread, so please figure out what you want, because there are so many ways to accomplish any layout/list task in Hugo.

Do you want a different layout for each content type? Do you want a page that lists each content type? How many content types are you planning on having?

Thank you for taking the time and explaining this to me. I understand my request might seem overloaded. So I am just simplifying it to the core.

I just want a page that lists posts from only a specific content type. I plan to use a maximum of 4 types. In short, 4 pages with 4 lists.

The super easy way to get what you want is to have four sections. Then they just create the pages you want. You want example.com/quotes, create /content/quotes and load it up with quote files. Do that for each of your 4 types.

If you want to post everything as one section, like blog for example, but want each blog post to look different based on type, you can add types to your content. However, that won’t create first-level folders like example.com/quotes. You’ll have to create those pages manually, and then you might be missing some functionality.

Of the two options, and since you have a number in mind, I’d go ahead and create four sections.