Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to list all category from custom post type?

Tags:

wordpress

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.

like image 533
ilovebali Avatar asked Sep 23 '16 03:09

ilovebali


3 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.

like image 152
deemi-D-nadeem Avatar answered Nov 02 '22 03:11

deemi-D-nadeem


    <?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.

like image 12
Husain Ahmed Avatar answered Nov 02 '22 03:11

Husain Ahmed


<?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;  ?>
like image 1
Purnendu Sarkar Avatar answered Nov 02 '22 02:11

Purnendu Sarkar