StackTips

How to List All the Categories for Custom Post Type

stacktips avtar

Written by

Editorial,  1 min read,  2.84K views, updated on Sept. 17, 2023

If you just want to list the custom post categories (taxonomies), then you can use the get_terms function.

The get_the_terms filter will be called when the cache has the term and will pass the found term along with the array of $taxonomies and array of $args.

<?php $taxonomy = 'books'; ?>
<?php $terms = get_the_terms($post_id, $taxonomy);?>
<?php if ($terms && !is_wp_error($terms)) : ?>
    <div class="tagcloud">
        <?php foreach ($terms as $term): ?>
            <a href="<?php echo get_term_link($term->slug, $taxonomy) ?>"><?php echo $term->name ?></a>
        <?php endforeach; ?>
    </div>
<?php endif;?>