如何在WordPress中显示所有类别? [英] how to display all categories in wordpress?

查看:70
本文介绍了如何在WordPress中显示所有类别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了以下代码:

      $categories = wp_get_post_categories(get_the_ID());
      foreach($categories as $category){
          echo '<div class="col-md-4"><a href="' . get_category_link($category) . '">' . get_cat_name($category) . '</a></div>';
        }

但只返回一个类别,我如何获得所有类别?

but return only one category, how can i get all the categories?

推荐答案

在您给我们的代码中,您选择了为特定帖子get_the_ID()选择的类别。但是,最好使用另一个函数get_categories() https://developer.wordpress.org/ reference / functions / get_categories / ,您可能会这样:

In the code you gave us you are selected the categories selected for the specific post get_the_ID() is doing that part. However you would be best off using another function get_categories() https://developer.wordpress.org/reference/functions/get_categories/ which you would do like so:

$categories = get_categories();
foreach($categories as $category) {
   echo '<div class="col-md-4"><a href="' . get_category_link($category->term_id) . '">' . $category->name . '</a></div>';
}

您还可以传递参数以更具体(如果需要)-请参见 https://developer.wordpress.org/reference/functions/get_terms/ 了解详情关于您可以通过的内容

You can also pass through arguments to be more specific (if needed) - see https://developer.wordpress.org/reference/functions/get_terms/ for details on what you can pass through

这篇关于如何在WordPress中显示所有类别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆