StackTips
 1 minutes

How to List All the Categories for Custom Post Type

By Editorial @stacktips, On Sep 17, 2023 Wordpress 2.31K Views

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;?>
stacktips avtar

Editorial

StackTips provides programming tutorials, how-to guides and code snippets on different programming languages.