Get all categories from custom post type in WordPress

Get all categories from custom post type in WordPress

In this blog I will get all the categories of custom post type using taxonomy. For this I've created post type books using Post Type Generator nd my taxonomy is post_book. To get all categories of custom post type I'm going to pass arguments in get_terms() function.
Use below code to get categories of custom post types `books`

<div class="container">
    <div class="row">
        <?php
            $args = array(
                        'taxonomy' => 'post_book',
                        'number' => '-1',
                        'orderby' => 'name',
                        'order'   => 'ASC'
                    );

            $cats = get_categories($args);
            foreach($cats as $cat) {
            ?>
                <div class="col-md-4">
                    <div class="inner_column">
                        <a title="<?php echo $cat->name; ?>" href="<?php echo get_category_link( $cat->term_id ); ?>?">
                            <h3><?php echo $cat->name; ?></h3>
                        </a>
                        <p><?php echo $cat->description; ?></p>
                        <a title="<?php echo $cat->name; ?>" class="readmore" href="<?php echo get_category_link( $cat->term_id ) ?>"> READ MORE </a>
                    </div>
                </div>
            <?php
            }
        ?>
    </div>
</div>

 

Also Read: Get all categories title by post id in WordPress
Also Read: Create separate search forms for products, posts types and posts in WordPress

In this blog I will get all the categories of custom post type using taxonomy. For this I've created post type books using Post Type Generator nd my taxonomy is post_book. To get all categories of custom post type I'm going to pass arguments in get_terms() function.
Use below code to get categories of custom post types `books`

<div class="container">
    <div class="row">
        <?php
            $args = array(
                        'taxonomy' => 'post_book',
                        'number' => '-1',
                        'orderby' => 'name',
                        'order'   => 'ASC'
                    );

            $cats = get_categories($args);
            foreach($cats as $cat) {
            ?>
                <div class="col-md-4">
                    <div class="inner_column">
                        <a title="<?php echo $cat->name; ?>" href="<?php echo get_category_link( $cat->term_id ); ?>?">
                            <h3><?php echo $cat->name; ?></h3>
                        </a>
                        <p><?php echo $cat->description; ?></p>
                        <a title="<?php echo $cat->name; ?>" class="readmore" href="<?php echo get_category_link( $cat->term_id ) ?>"> READ MORE </a>
                    </div>
                </div>
            <?php
            }
        ?>
    </div>
</div>

 

Also Read: Get all categories title by post id in WordPress
Also Read: Create separate search forms for products, posts types and posts in WordPress

Please let me know what your thoughts or comments are on this article. If you have any suggestion or found any mistake in this article then please let us know.

Latest Comments

Add your comment

Close