How to get all taxonomy terms

Get all taxonomy terms for custom post type is super easy. Here you’ll learn to get those from a one single line of code. If you want to learn how to register a post type for a taxonomy please visit our article on how to created posts types using a plugin. Also you can follow this article about how to create taxonomy in WordPress to create taxonomies.
// My taxonomy_name is foods $foods = get_terms('foods');
By analyzing this code you’ll be able to list all taxonomy terms within WordPress.
$all_terms
is the defined PHP variable to assign all the terms return from the taxonomy_name
. Here you can print all the terms before proceed with the coding. That will showing up all the taxonomy terms related to the taxonomy term. Let’s see how.
foreach ($foods as $food) { echo '<h2>'. $food->name . '</h2>'; }
You just need to print the variable. It will showing up all the term details. Here is an example of mine.
This will showing up the all taxonomy terms assigned to the foods
. One is fruits
and other one is vegetables
.
How to code to get these single terms
You can get all of them via a foreach
loop. Please refer the code below to understand it simply.
foreach ($foods as $food) { echo '<h2>'. $food->name . '</h2>'; }
This will list the term names Fruits
and Vegetables
within h2
tag.
Hope you enjoyed this tutorial. Happy learning ahead. Thanks for reading.
Recommended: Please follow our step by step guide how to create a WordPress Theme from scratch. You’ll be able to create your own theme at the end of these lessons.