如何列出自定义帖子类型的所有类别?

时间:2016-09-23 03:31:01

标签: wordpress

我有一个名为'dining'的帖子类型,并且有一个名为'dining-category'的分类。

我想要做的是,我想在我的页脚区域中显示所有类别的“餐饮”类别。

4 个答案:

答案 0 :(得分:14)

在WordPress 4.6中get_terms已被弃用。所以有一个替代(get_categoriesRead this

以下是示例代码:

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

希望这会对你有所帮助。

答案 1 :(得分:5)

    <?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; ?>cc</a></li>
         <?php
       }
       echo '</ul>';
   ?>

如果类别未分配任何帖子,则不会显示。因此分配任何帖子。这段代码运行得很好。

答案 2 :(得分:0)

使用get_terms()函数通过slug获取自定义分类法,在你的情况下slug是用餐类别。 从wordpress codex网站阅读函数参考并试试这个。

答案 3 :(得分:-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;  ?>
相关问题