2013-11-25 161 views
2

有没有更好的方式在wordpress中获取自定义帖子类型的类别名称?wordpress,获取自定义帖子类型的类别名称

<?php  // get the portfolio categories 
      $terms = get_the_terms($post->ID, 'filters');       
      if ($terms && ! is_wp_error($terms)) : 
       $names = array(); 
       $slugs = array(); 
       foreach ($terms as $term) { 
       $names[] = $term->name; 
       $slugs[] = $term->slug; 
       }         
       $name_list = join("/", $names); 
       $slug_list = join(" category-", $slugs); 
     endif; 
    ?> 


    <!-- BEGIN portfolio-item--> 
    <li class="portfolio-item third column category-<?php echo $slug_list; ?>" data-filter="category-<?php echo $slug_list; ?>"> 

回答

11
<?php 
$taxonomy = 'filters'; 
$terms = get_terms($taxonomy); 

if ($terms && !is_wp_error($terms)) : 
?> 
    <ul> 
     <?php foreach ($terms as $term) { ?> 
      <li><a href="<?php echo get_term_link($term->slug, $taxonomy); ?>"><?php echo $term->name; ?></a></li> 
     <?php } ?> 
    </ul> 
<?php endif;?> 

还是在fuctions.php地方是:

function get_the_category_custompost($id = false, $tcat = 'category') { 
    $categories = get_the_terms($id, $tcat); 
    if (! $categories) 
     $categories = array(); 

    $categories = array_values($categories); 

    foreach (array_keys($categories) as $key) { 
     _make_cat_compat($categories[$key]); 
    } 

    return apply_filters('get_the_categories', $categories); 
} 

,并呼吁作为函数:

<?php $cat = get_the_category_custompost($post->ID, 'Your Custom Taxonomy'); ?> 
+1

感谢您的帮助;非常感激 –

3

我的回答似乎过于简单,但我用这个列表wordpress插件名为DW Question Answer的类别与标准wp类别具有不同的类别。

我假设设计墙使用自定义帖子类型来创建零件和分类来创建类别。

<ul> 
    <?php wp_list_categories('taxonomy=dwqa-question_category&hide_empty=0&orderby=id&title_li=');?> 
</ul> 
0

假设您的自定义分类法recipegroups,我已经实现和测试的functions.php这个代码,我相信它会在插件工作了。

$recipeTerms = get_terms(array( 
    'taxonomy' => 'recipegroups', 

)); 
     foreach($recipeTerms as $recipeTerm){ 
      if($recipeTerm->parent==0){ 
       echo "<div class='termsBox'>"; // remove these div's to suit your needs.. 

       $termLink =get_term_link($recipeTerm); 
       echo "<a href='$termLink'><div class='termParent'>".$recipeTerm->name."</div></a> "; 

       $termChilds = get_term_children($recipeTerm->term_id, 'recipegroups'); 
       foreach($termChilds as $child){ 
        $chTerm = get_term_by('id', $child, 'recipegroups'); 
        $termLink =get_term_link($chTerm); 
        echo "<a href='$termLink'><div class='top-cat-items'>".$chTerm->name."</div></a>"; 
        } 
        echo "</div>"; // end of termsBox div 
       } 

      } 
相关问题