For each loop on every word in post

I'm having a recipe site where every post naturally contains ingredients. Every ingredient is a tag, and I would like to automatically link every ingredient so the user can click on it and see all recipes that uses that certain ingredient.

For this to be possible I guess I have to loop through every word in the post, then check if that word is equal to an existing tag, and wrap that word in a hyperlink. But I'm not sure that's the most efficient solution.

Do anyone of you have any suggestions here?

Topic terms loop tags Wordpress

Category Web


Are you sure you want to link every instance of each ingredient? As a user, I wouldn't mind if the ingredient list above the recipe had all its ingredients linked, but if the following instructions continued linking every ingredient I'd find that to be a lot of links I could accidentally tap on and lose the recipe.

If you're using the Block Editor, you could use the Tag List block to list the ingredients wherever in the recipe posts you need them. (Optionally you could add a block style to style them differently if you like.) If you're not using the Block Editor, you might want to set up a custom template for recipes, so that the template automatically creates an ingredients section between the recipe title and the rest of the content and outputs the tags there. If you're tagging anything besides ingredients, you could create a custom taxonomy so you're only outputting ingredients and not other unrelated tags.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.