How to List All Category from Custom Post Type?

I have a post type called 'dining' and has a taxonomy called 'dining-category'.

What I want to do is, I want to display all the category from post type 'dining' in my footer area.

4 Answers

In WordPress 4.6 get_terms is deprecated. So there is an alternate of this (get_categories) Read this

And here is Example code:

<?php
   $args = array(
               'taxonomy' => 'dining-category',
               'orderby' => 'name',
               'order'   => 'ASC'
           );

   $cats = get_categories($args);

   foreach($cats as $cat) {
?>
      <a href="<?php echo get_category_link( $cat->term_id ) ?>">
           <?php echo $cat->name; ?>
      </a>
<?php
   }
?>

Hope this will help you.

1
    <?php
       $args = array(
       'type'                     => 'dining',
       'child_of'                 => 0,
       'parent'                   => '',
       'orderby'                  => 'name',
       'order'                    => 'ASC',
       'hide_empty'               => 1,
       'hierarchical'             => 1,
       'taxonomy'                 => 'dining-category',
       'pad_counts'               => false );
       $categories = get_categories($args);
       echo '<ul>';

       foreach ($categories as $category) {
         $url = get_term_link($category);?>
          <li><a href="<?php echo $url;?>"><?php echo $category->name; ?></a></li>
         <?php
       }
       echo '</ul>';
   ?>

If category not assigned any post it will not show. therefore assign any post. This code running perfectly.

1
<?php 
$wcatTerms = get_terms(
'category', array('hide_empty' => 0, 'number' => 3, 'order' =>'asc', 'parent' =>0));
        foreach($wcatTerms as $wcatTerm) : 
    ?>
            <small><a href="<?php echo get_term_link( $wcatTerm->slug, $wcatTerm->taxonomy ); ?>"><?php echo $wcatTerm->name; ?></a></small>
                <?php
                    $args = array(
                        'post_type' => 'post',
                        'order' => 'ASC',
                        'tax_query' => array(
                            array(
                                'taxonomy' => 'category',
                                'field' => 'slug',
                                'terms' => $wcatTerm->slug,
                            )
                        ),
                        'posts_per_page' => 1
                    );
                    $loop = new WP_Query( $args );
                    while ( $loop->have_posts() ) : $loop->the_post();
                    $imgurl = get_the_post_thumbnail_url( get_the_ID(), 'full' );
                    $title=get_the_title($post->ID);
                ?>
              <a href="<?php the_permalink(); ?>">
                <?php the_title(); ?>
              </a>
            <?php endwhile; wp_reset_postdata(); ?> 
     <?php endforeach;  ?>
1

use get_terms() function to fetch custom taxonomy by its slug, in your case slug is dining-category. read function refrence from wordpress codex website and try this.

2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

James H. Sterling

James H. Sterling

Environmental Science & Climate Journalist

James Sterling reports on renewable energy developments, climate policy, ecological conservation, and green tech innovations around the globe.

Share this article
Twitter Facebook Pinterest