2017-07-17 51 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

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