how to display all categories in wordpress?

前端 未结 3 1707
逝去的感伤
逝去的感伤 2020-12-15 05:29

I used this code:

      $categories = wp_get_post_categories(get_the_ID());
      foreach($categories as $category){
          echo \'
3条回答
  •  佛祖请我去吃肉
    2020-12-15 05:56

    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

    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'           => '
    ', '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) );

提交回复
热议问题