2017-07-17 61 views
0

我註冊了一些分類標準,並希望檢索它們的分類標籤以列出其中的一些分類標籤。該列表不是與帖子或類別相關的,並且幾乎可以出現在任何地方(單個,存檔等)。列出某些分類標準

我想創建一個可變/所討論的分類法的陣列,所以我可以在其他地方使用它:

$tax_names = array('tax_01', 'tax_02', 'tax_03'); 

此代碼的工作,但只輸出一個分類法:

$tax_args = array(
    'name' => 'tax_01' 
); 

$output = 'objects'; 

$taxonomies = get_taxonomies($tax_args, $output); 

if ($taxonomies) { 
    foreach ($taxonomies as $taxonomy) { 
    echo '<p>' . $taxonomy->label . '</p>'; 
    } 
} 

這不起作用:

$tax_args = array(
    'name' => $tax_names // using the array created above 
); 

$output = 'objects'; 

$taxonomies = get_taxonomies($tax_args, $output); 

if ($taxonomies) { 
    foreach ($taxonomies as $taxonomy) { 
    echo '<p>' . $taxonomy->label . '</p>'; 
    } 
} 

任何幫助表示讚賞。

回答

0

如果你想使用它的任何地方一樣,你可以在functions.php的像

retrieve_my_terms(); 

編輯,創建一個功能

function retrieve_my_terms() { 

global $terms; 

$terms = get_terms('taxonomy'); 

foreach ($terms as $term) { 
    $option = $term->name; 
    return $option; 
} 
} 

然後在你的PHP文件,你可以調用函數:

你可以嘗試這樣做 - 我沒有正確測試它,但它可能對你有幫助。

function retrieve_my_terms($termArray) { 

    global $terms; 

    $terms = $termArray; 

    foreach ($terms as $term) { 
     $option = get_term_by($term); 
     return $option; 
    } 
} 


$tax_names = array('tax_01', 'tax_02', 'tax_03'); 
retrieve_my_terms($tax_names); 
+0

感謝您的幫助!這有效,但它返回分類術語。我想回顯存儲在數組中的分類標籤,例如: '$ tax_names = array('tax_01','tax_02','tax_03');' 輸出應該是分類標籤:Taxonomy 01,Taxonomy 02 ,Taxonomy 03. – george

+0

我已經更新了我的回答... –