2013-11-25 201 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 
       } 

      } 
相關問題