HUGO

Migrating from Jekyll, help with Taxonomies and specific content loops

I’m in the process of moving my blog from Jekyll to Hugo, so Taxonomies are something new to me, but I believe it’s what I need to be working with.

The site in it’s half complete state is here: https://github.com/breadcat/blog.minskio.co.uk

What I want to achieve is to assign a tag or value to each of my recipes, so then I can make a loop based on that tag on the recipes/_index.md page. The existing jekyll code is still in place, I’m wanting to replace it pretty much like for like.

I’ve also noticed that when I add pages with their actual names spaghetti-carbonara.md they get included in the blog posts. Will they always need to be named spaghetti-carbonara\_index.md?

Hi and welcome

I had a look at your github repo - and wonder have you got your site running locally yet? or does the build fail?

each of your recipes will have front matter - i see you already have added a taxonomy called type on your cabonara receipe

---
layout: page
title: Spaghetti Carbonara
type: recipe_meal
---

I think you dont need type: recipe_meal as that is kind of obvious from the folder

maybe you want to have a taxonomy called ingredients instead?

like …

---
layout: page
title: Spaghetti Carbonara
type: recipe_meal
ingredients: keto pasta, lamb mince, carrot, celery, white wine, milk
country: Italy
---

Finally … I was not sure why you are writing code in the _index.md file ? I think this is your second question? … the code should be in theme / layouts

But …depending on the Hugo theme you have - it should already support generating taxonomy pages for you

then all you need to do is add your custom taxonomies to your config.toml …

Thanks Damien,

The code on github builds fine, I’m developing the site locally without issue.

I think you may have misunderstood my aim. What I want to do is have a tag I can reference to build a list of pages that have contain that tag.

If you look at the current liquid code in recipes.md (I appreciate it may need moving), it will check every page for type: recipe_meal then build a list of all those pages, same for recipe_snack and all other tags I categories recipes using.

The reason I’m doing this is because I have a number of ‘sub pages’ like the above, if you check my current Jekyll blog you’ll see what it produces.

The theme I’m using is one I made myself, so it may not support taxonomies like a ‘proper’ theme would.

My second question is more about file naming and permalinks, if I add spaghetti-carbonara.md, it will appear in my list of blog posts. If I make add it under a directory spaghetti-carbonara\_index.md it does not. I want to know if this is the proper practice for making these ‘sub pages’.

Hi @dracco

I understand what you are talking about and this working on my site nothing strange there - it works out of the box… it has tags and categories – these are built in the them

I added a custom taxonomy series – the custom taxonomy links related articles … series example

Hugo is clever - you dont need to put type: recipe_meal in your front matter … so long as your recipes are saved in the folder called recipes under the content folder … then you just build a query that looks for the content in that folder …

keep your hugo content folders flat -
top level … /content/
has a subfolder
/recipes/
_index.md
keto-stirfry.md
keto-ragu.md
keto-pancakes.md

checkout

hope that helps?

I’m not sure you’re getting what I want to achieve.

My intention is to split recipes by category, as in my example above. Your series example does not split by category.

@dracco

Just create a custom taxonomy - for meal types

You then do a query to find recipes matching that type

so your front matter looks like

---
title: Spaghetti Carbonara
mealtype: Dinner
ingredients: keto pasta, lamb mince, carrot, celery, white wine, milk
country: Italy
---

Okay, so I add a custom taxonomy, how do I build a list based on this value? This is the core of what I want.

Hi @dracco

theres lots of good docs … but specifically this page has loads of code examples

Hint - try this to get started with a list of all taxonomies and [related content] - add this to the layout for the recipes section.

(https://gohugo.io/templates/taxonomy-templates/#example-list-all-taxonomies-terms-and-assigned-content) …

failing that … a very simple query will be …

<h3>Dinner Posts</h3>
	<ul>
	  {{ range first 3 .Site.Taxonomies.mealtype.dinner }}
		  <li><a href="{{ .Page.RelPermalink }}">{{ .Page.Title }}</a>
		  </li>
	  {{ end }}
	</ul>

Thanks for your help again, I feel as though I’m getting fairly close now but I’m still not getting anything rendering when I add that code.

I’ve assigned a respective recipetype: value to each of my recipes, added these values to my config.toml, assigned layout: recipes to my recipes\_index.md page and added your code above, but made tweaks based on this post.

	<ul>
	  {{ range first 3 .Site.Taxonomies.recipetype.meal }}
		  <li><a href="{{ .Page.RelPermalink }}">{{ .Page.Title }}</a>
		  </li>
	  {{ end }}
	</ul>

I still get no output however, when I use the full " Example: List All Taxonomies, Terms, and Assigned Content" code, I do get output, but none of my pages seem to be assigning their taxonomies correctly.

I haven’t pushed this latest code to github, but if you’d like me to, I’ll create a separate branch for it.

1 Like

@dracco Glad to see you making progress

So i think you need to do one more thing – have you added recipetype to your config as a custom taxonomy

like this ,

[taxonomies]
    category = "categories"
    recipetype = "recipetype"
    tag = "tags"

BTW I love the keto diet too and was playing around with recipes and showing a list of the first 3 in this git repo - lines 20 - 36

Ah, perfect! I was being an idiot and adding the values (bread) instead of the taxonomy (recipetype), it’s working perfectly now as expected!

I appreciate you sticking with me, thanks again!

1 Like

Let me know when the site is live - id love to have a look

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