2015-03-02 44 views
0

我想顯示一個自定義帖子類型的分類列表,不包括那些不是分層的列表。顯示分類列表,只有它們是分層的

下面的代碼目前的工作,但顯示所有的分類法,但我不能得到它來檢查分類是否分層次之前,通過循環運行它。

我知道有運行此檢查一個WordPress的功能,但由於我平時工作的前端,我似乎無法搞清楚在哪裏把它,以便使其生效:

is_taxonomy_hierarchical($taxonomy) 

以下是我使用的輸出的分類列表功能:

// get taxonomies terms links 
function custom_taxonomies_terms_links(){ 

    // get post by post id 
    $post = get_post($post->ID); 

    // get post type by post 
    $post_type = $post->post_type; 

    // get post type taxonomies 
    $taxonomies = get_object_taxonomies($post_type, 'objects'); 

    $out = array(); 

    echo '<a href="'; 
    echo '/'; 
    echo '">'; 
    echo 'Home'; 
    echo "</a>/"; 

    foreach ($taxonomies as $taxonomy_slug => $taxonomy){ 

    // get the terms related to post 
    $terms = get_the_terms($post->ID, $taxonomy_slug); 

    if (!empty($terms)) {  
     foreach ($terms as $term) { 
     $out[] = 
      '<a href="' 
     . get_term_link($term->slug, $taxonomy_slug) .'">' 
     . $term->name 
     . "</a>/"; 
     } 
     $out[] = " "; 
    } 
    } 
return implode('', $out); 
} 

回答

0

如果我理解你正確,不能你只是測試類似下面的分類:

foreach ($taxonomies as $taxonomy_slug => $taxonomy){ 

    if ($taxonomy->hierarchical) { 
     // get the terms related to post 
     $terms = get_the_terms($post->ID, $taxonomy_slug); 

     if (!empty($terms)) {  
      foreach ($terms as $term) { 
       $out[] = 
       '<a href="' 
      . get_term_link($term->slug, $taxonomy_slug) .'">' 
      . $term->name 
      . "</a>/"; 
      } 
      $out[] = " "; 
     } 
    } 
} 

當您使用「對象」作爲第二個參數:

get_object_taxonomies($post_type, 'objects'); 

你回來什麼是分類對象的數組,而不是僅僅分類名稱(其他選項)。分類對象具有「分層」屬性,指示該分類是否是分層的。您可以測試此選項以選擇您需要的分類法(分層或不分層)的類型。

+0

謝謝,這正是我所追求的。 有沒有什麼機會可以解釋你添加的語法,僅供將來參考? – Nervewax 2015-03-03 13:20:47

+0

很高興工作。我添加了一些額外的信息。 – bobdye 2015-03-03 16:14:56