这段代码95%可以工作,但我需要一些关于最后部分的帮助。我试图从Wordpress中获取所有自定义分类法和分类术语,并将它们显示在无序列表中。这是我的代码:
$args=array('public' => true, '_builtin' => false);
$output = 'names';
$operator = 'and';
$taxonomies=get_taxonomies($args,$output,$operator);
if ($taxonomies) {
foreach ($taxonomies as $taxonomy ) {
echo '<a>'. $taxonomy. '</a>';
$terms = get_terms("color");
$count = count($terms);
if ( $count > 0 ){
echo '<ul>';
foreach ( $terms as $term ) {
echo "<li>" . $term->name . "</li>";
}
echo "</ul>";
}
}
}
问题出现在第8行,它读取$terms = get_terms("color");
。我写这篇文章是为了测试代码,但问题是Wordpress现在为每个分类法显示分类法"颜色"中的术语。
我该如何修改此代码,以便对于Wordpress显示的每个分类法,它也会显示该分类法的相应术语列表?
$terms = get_terms($taxonomy);
在这种情况下,$taxonomy
不是一个对象,而是一个分类名称数组($output = 'names'
)。因此$taxonomy->name
不起作用。
参见:
http://codex.wordpress.org/Function_Reference/get_taxonomieshttp://codex.wordpress.org/Function_Reference/get_terms
我想显示所有分类法:
分类1-术语1a-术语1b-等等分类2-术语2a-术语2b-等等
这是代码:
<?php
$args=array('public' => true, '_builtin' => false);
$output = 'names';
$operator = 'and';
$taxonomies=get_taxonomies($args,$output,$operator);
if ($taxonomies) {
foreach ($taxonomies as $taxonomy ) {
echo '<a>'. $taxonomy. '</a>';
$terms = get_terms($taxonomy);
$count = count($terms);
if ( $count > 0 ){
echo '<ul>';
foreach ( $terms as $term ) {
$termlinks= get_term_link($term,$taxonomy);
?> <a href="<?php echo $termlinks; ?>">
<?php echo "<li>" . $term->name . "</li>"; ?></a><?php
}
echo "</ul>";
}
}
}
?>
$terms = get_terms($taxonomy->name);
?