简体   繁体   English

get_terms('类别')

[英]get_terms('category')

I am trying to display the categories with the get_terms function with the following code:我正在尝试使用以下代码使用 get_terms 函数显示类别:

$categories = get_terms( 'category');
$categories_count = count($categories);

                    for ($i = 0; $i < $categories_count; $i++) {
                        echo($i . $categories[$i]->name.' <br /> ');
                    }

But for some reason doesnt display all the categories, a couple of them are missing uncategorized and something else, this is what I see on the screen when i run this code:但是由于某种原因没有显示所有类别,其中一些类别丢失了未分类和其他内容,这是我在运行此代码时在屏幕上看到的内容:

  • 0 Chairs 0 把椅子
  • 1 1个
  • 2 Interiors 2 内饰
  • 3 3个
  • 4 Featured 4 精选

Thanks very much,非常感谢,

Your code is correct as far as I can tell, however the will be an issue in get_terms() OR the terms themselves don't actually have a name.据我所知,您的代码是正确的,但是这将是 get_terms() 中的一个问题,或者这些术语本身实际上没有名称。

You can try get_categories() instead of get_terms() .您可以尝试get_categories()而不是get_terms() This is how you should do it:你应该这样做:

<?php 
    $categories = get_categories(); 
    $number = 1;
    foreach ($categories as $category) {
        print $number . $category->cat_name . '<br />';
        $number++;
    }
?>

Just give it a shot...试一试...

To display tags and/or categories list:显示标签和/或类别列表:

<span>Tags: <?php 
                    global $post;
                    $terms = wp_get_post_terms($post->ID, 'your-taxonomy');
                    if ($terms) {
                        $output = array();
                            foreach ($terms as $term) {
                            $output[] = '<a href="' .get_term_link( $term->slug, 'your-taxonomy') .'\*for CPT please add url suffix /?post_types=your_cpt*\">' .$term->name .'</a>';
                            }
                            echo join( ', ', $output );
                                }
                                        ?>

                <span>

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

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