Home    Articles

 

How to fetch/get all categories in wordpress

 

I saw various code lines that you can use to do this, but it didn't helped me. I needed all categories, not what get_categories function returns. All i needed was the category id and category name.

Of course you can use this WP function called get_categories but if this function doesn't work for you, check this code. Basicly i use get_terms function.

   $categories = $terms = get_terms(array(
        'taxonomy' => 'category',
        'hide_empty' => false,
        'orderby'=>'name',
        'order'=>'ASC'
    ));
    echo '<ul>';
    foreach( $categories as $category ) {
        echo '<li><input type="checkbox" name="category[]" value="'.$category->term_id.'" /><label>'.$category->name.'</label></li>';
    }
    echo '</ul>'

 

by