繁体   English   中英

Wordpress:获取自定义分类和分类术语

[英]Wordpress: Get custom taxonomies and taxonomy terms

此代码有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显示的每个分类法,它还会显示该分类法的相应术语列表?

colindunnn,谢谢你的代码伙伴。 当尝试做类似的事情时它帮了很多...我想显示所有分类:

Taxonomy1
-Terms1a
-Terms1b
-etc

Taxonomy2
-Terms2a
-Terms2b
-etc

这是代码......如果有人需要它们。

<?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);

在这种情况下,$ taxonomy没有对象,只是一个分类名称的数组($ output ='names')。 因此$ taxonomy-> name不起作用。

看到:

http://codex.wordpress.org/Function_Reference/get_taxonomies http://codex.wordpress.org/Function_Reference/get_terms

$terms = get_terms($taxonomy->name);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM