How To Loop Through Post Categories In WordPress

Trying to loop through post categories? All you may need is to use the get_categories() function to loop through them with a PHP foreach loop.

Looping through WordPress post categories

To loop through WordPress post categories, copy and paste the code below into the file you wish to display them on.

foreach ( get_categories() as $category ) :
     echo $category->name;

get_categories() not working?

Is your get_categories() function not showing any matches? You may need to allow the display of empty categories. In order to solve this, try replacing the first line of the foreach loop above with this one below.

foreach ( get_categories( array( 'hide_empty' => false ) ) as $category ) :