wordpress 如何在wordpress中显示所有类别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39704715/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 11:14:45  来源:igfitidea点击:

how to display all categories in wordpress?

wordpresscategories

提问by Clorge32

I used this code:

我使用了这个代码:

      $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?

但只返回一个类别,我怎样才能获得所有类别?

回答by Simon Pollard

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:

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

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

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

您还可以传递更具体的参数(如果需要) -有关您可以传递的内容的详细信息,请参阅https://developer.wordpress.org/reference/functions/get_terms/

回答by Josh Bradley

You can also use wp_list_categories and pass arguments to it to show only what you need. A full list of arguments can be found in the codex: https://developer.wordpress.org/reference/functions/wp_list_categories

您还可以使用 wp_list_categories 并将参数传递给它以仅显示您需要的内容。完整的参数列表可以在代码中找到:https: //developer.wordpress.org/reference/functions/wp_list_categories

This will output all categories (even if they're empty) indented to indicate hierarchy.

这将输出缩进以指示层次结构的所有类别(即使它们是空的)。

$args = array(
    'child_of'            => 0,
    'current_category'    => 0,
    'depth'               => 0,
    'echo'                => 1,
    'exclude'             => '',
    'exclude_tree'        => '',
    'feed'                => '',
    'feed_image'          => '',
    'feed_type'           => '',
    'hide_empty'          => 0,
    'hide_title_if_empty' => false,
    'hierarchical'        => true,
    'order'               => 'ASC',
    'orderby'             => 'name',
    'separator'           => '<br />',
    'show_count'          => 0,
    'show_option_all'     => '',
    'show_option_none'    => __( 'No categories' ),
    'style'               => 'list',
    'taxonomy'            => 'category',
    'title_li'            => __( 'Categories' ),
    'use_desc_for_title'  => 1,
);

var_dump( wp_list_categories($args) );

回答by WordpressDave

like this :

像这样 :

<?php
$categories = get_categories( array(
    'orderby' => 'name',
    'order'   => 'ASC'
) );

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