Next Previous

WP Get Categories

78

Get All Categories

To get all the categories, including subcategories data without any HTML, call get_categories() function with no arguments.

This will return an array of Category objects.

Loop through an array and print individual category name and ID.

foreach (get_categories() as $cat) {
    echo '<div style="border:1px solid blue">';
    echo 'category name = ' . $cat->name . '<br>';
    echo 'category ID = '. $cat->cat_ID . '<br>';
    echo '</div>';
}

And the output would look like this:

Display All Categories

If you want to get all categories with preformatted HTML, call wp_list_categories() function.

This function organizes categories and subcategories in an unordered lists format.

Next Previous