Hugo

Is it possible to get every category dynamically to display it?

I’m looking at the docs and the only thing that I can find is how to display the tags and categories: https://gohugo.io/variables/taxonomy/#example-usage-of-site-taxonomies

What I want to do is be able to automatically search each data item and take all of it’s categories and display them (removing any duplicates) same with the tags. Is this possible with the API or will I have to rig something up myself?

What you are describing is creating a template, and as luck would have it, we have taxonomy templates, which happens to have plenty of code samples. :slight_smile:

Yes I’ve read through that, and I cant seem to find my specific question, as far as I’m aware I would have to create a list template and not be able to dynamically add these items.

So unless I’m missing something is it possible to just read in all my data objects and just pull each category out without having to pre specify a list of items?

Yes, it is possible, a common thing. I suggest you follow some tutorials and try using Hugo, and then show what you’ve tried when you hit a wall. Follow the other suggestions at Requesting Help.

All the tutorials I’ve seen grab from a list of taxonimys from a pre defined list, could you point me in the right direction then?

Please explain what that means. Point to a URL and quote the code that is doing what you say, because I don’t understand, and the taxonomy system in Hugo is fairly straightforward.

I’m pretty sure I’m knowledgeable about the taxonomy system, what I’m trying to do is grab every category of every data item object in my data folder and use that to populate a navbar dynamically (not have to set up a list of items)

Item1{
“category” : “foo”
}

Item2{
“category” : “bar”
}

Let’s say I have those two items in my data folder, what I want to do is create a function that will go through both of those items parse the categories and add them [foo, bar] to a navbar without any duplicates. Doing it this way gives me the ability to let my clients dynamically add product categories to their website without me having to go through and manually put in taxonomy category.

The way it normally works is a person puts their categories in the front matter of the content, such a blog post or store item. Then Hugo generates the taxonomy pages as needed. You don’t need to go in and add anything manually.

I suspect there is a way to do what you are asking, but I don’t fully understand, so I’ll step aside and let the floor take a look.

Yes, all I’m trying to do is generate that front matter dynamically. Thanks for any help!

Can you describe the steps the client will take to do so?

All they would do is create a item in the data folder:

Item1{
    "title": "Some title",
    "category" : foo
} 

Item2{
     "title": "Some title 2",
     "category": bar
} 

Item3{
    "title": "Some title 3",
    "category": foobar
} 

Item4{
    "title": "Some title 4",
    "category": foobar
} 

And from there I was hoping I could just generate a nav bar with the three items dynamically that contain [foo, bar, foobar] without having to go in myself and updating the list to include any of the words [foo, bar, foobar]. Basically it would auto generate the list for me.

The wording I used in your question was meant for the actual taxonomy not to dynamically add items to the data folder. Sorry for the confusion.

Did you look at the casper-two theme yet? If the taxonomy of a post has a category when you go to the category menu link this theme automatically generates the category with the number of corresponding posts. For example: Foo (1) Bar (1) Foobar (2)

1 Like