简体   繁体   English

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

[英]Wordpress: Get custom taxonomies and taxonomy terms

This code is 95% working, but I need some help with the last part. 此代码有95%可用,但我需要一些帮助。 I am trying to fetch all the custom taxonomies and taxonomy terms from Wordpress and display them in an unordered list. 我试图从Wordpress获取所有自定义分类和分类术语,并将它们显示在无序列表中。 Here is my code: 这是我的代码:

$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>";
            }
          }
        }

The problem is on line 8 where it reads $terms = get_terms("color"); 问题出在第8行,它读取$terms = get_terms("color"); . I wrote this as a means to test the code but the problem is that Wordpress now displays the terms from the taxonomy 'color' for every taxonomy. 我写这个作为测试代码的方法,但问题是Wordpress现在显示每个分类法的分类法“颜色”的术语。

How would I modify this code so that for each taxonomy Wordpress displays, it would also display the corresponding list of terms for that taxonomy? 我如何修改此代码,以便对于Wordpress显示的每个分类法,它还会显示该分类法的相应术语列表?

colindunnn, thank for your code mate. colindunnn,谢谢你的代码伙伴。 It helped alot when trying to do a similar thing... i wanted to display for all taxonomies: 当尝试做类似的事情时它帮了很多...我想显示所有分类:

Taxonomy1
-Terms1a
-Terms1b
-etc

Taxonomy2
-Terms2a
-Terms2b
-etc

here is the code... if anyone needs them. 这是代码......如果有人需要它们。

<?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 is in this case no object but simply an array of the taxonomy names ($output = 'names'). 在这种情况下,$ taxonomy没有对象,只是一个分类名称的数组($ output ='names')。 Therefore $taxonomy->name does not work. 因此$ taxonomy-> name不起作用。

See: 看到:

http://codex.wordpress.org/Function_Reference/get_taxonomies http://codex.wordpress.org/Function_Reference/get_terms 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