17

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 4

40

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
  • Don't forget 'hide_empty' => false in the args if you would like to show custom categories not specifically tied to a CPT. Apr 7, 2022 at 20:33
13
    <?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
  • how add parent and child to make different parent and child categories inside <li> using get_categories(). Oct 19, 2018 at 6:44
2
<?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
  • 1
    While this code snippet may be the solution, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. Nov 15, 2018 at 11:15
-2

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
  • get_terms() is depreciated in WordPress 4.6
    – Omer
    Sep 23, 2016 at 12:22
  • 1
    he doesn't mentioned wp version Sep 24, 2016 at 7:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.