September 19, 2016

How to show a list of all available categories on one page.

Here’s a neat little thing. I had all the categories of a custom post type on the page floating around dynamically like a tag cloud as an animated design element. It made the processor choke like a horror show, but the overall effect was cool. Note to self. Figure out how to put this into canvas.

    <?php 
    $args = array (
        'taxonomy' => 'employee-type', //your custom post type
        'orderby' => 'name',
        'order' => 'ASC',
        'hide_empty' => 0 //shows empty categories
    );
    $categories = get_categories( $args );
    echo '<ul class="floating-cats one">';
    foreach ($categories as $category) {    
        $post_by_cat = get_posts(array('cat' => $category->term_id));
        echo '<li>' . $category->name . '</li>';
    };
    echo '</ul>';
    ?>
List All Categories of Custom Post Type