Title: List Categories in an Array
Last modified: August 19, 2016

---

# List Categories in an Array

 *  Resolved [djeff8593](https://wordpress.org/support/users/djeff8593/)
 * (@djeff8593)
 * [17 years, 1 month ago](https://wordpress.org/support/topic/list-categories-in-an-array/)
 * I need to list my categories in an array like so:
 * “Category 1”, “Category 2”, “Category 3”, “Category 4”
 * Can anyone provide a piece of code that might help? I saw this post:
    [http://wordpress.org/support/topic/222394?replies=3](http://wordpress.org/support/topic/222394?replies=3)
 * But it wasn’t quite what I was looking for.
 * Any advice would be greatly appreciated.

Viewing 2 replies - 1 through 2 (of 2 total)

 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [17 years, 1 month ago](https://wordpress.org/support/topic/list-categories-in-an-array/#post-1024368)
 *     ```
       <?php
       //display all categories that have at least one post, separate each category with comma+space, put each category in quotes
       $categories=get_categories();
       if ($categories) {
       $cat_count =count($categories);
       $output = '';
       foreach($categories as $category) {
           if ($category->count > 1) {
           $output .= '"<a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a>", ';
       }
       }
       }
       echo '<p>' . substr($output, 0, -2) . '</p>';  // display all but the last comma
       ?>
       ```
   
 *  Thread Starter [djeff8593](https://wordpress.org/support/users/djeff8593/)
 * (@djeff8593)
 * [17 years, 1 month ago](https://wordpress.org/support/topic/list-categories-in-an-array/#post-1024445)
 * Thanks so much Michael. I’ll give it a go.
 * Best – David

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘List Categories in an Array’ is closed to new replies.

## Tags

 * [array](https://wordpress.org/support/topic-tag/array/)
 * [categories](https://wordpress.org/support/topic-tag/categories/)
 * [list](https://wordpress.org/support/topic-tag/list/)
 * [meh_code](https://wordpress.org/support/topic-tag/meh_code/)

 * 2 replies
 * 2 participants
 * Last reply from: [djeff8593](https://wordpress.org/support/users/djeff8593/)
 * Last activity: [17 years, 1 month ago](https://wordpress.org/support/topic/list-categories-in-an-array/#post-1024445)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
