A
A
Alexander Osadchy2020-07-06 22:10:24
WordPress
Alexander Osadchy, 2020-07-06 22:10:24

How to display a list of categories from the Custom post type in the template?

My CPT

add_action( 'init', 'register_works_post_type' );
function register_works_post_type() {
  // Раздел вопроса - workscat
  register_taxonomy('workscat', array('works'), array(
    'label'                 => 'Категории', // определяется параметром $labels->name
    'labels'                => array(
      'name'              => 'Категории работ',
      'singular_name'     => 'Категории',
      'search_items'      => 'Искать категорию',
      'all_items'         => 'Все категории',
      'parent_item'       => 'Родит. категория',
      'parent_item_colon' => 'Родит. категория:',
      'edit_item'         => 'Ред. категорию',
      'update_item'       => 'Обновить категорию',
      'add_new_item'      => 'Добавить категорию',
      'new_item_name'     => 'Новый Раздел вопроса',
      'menu_name'         => 'Категории',
    ),
    'description'           => 'Рубрики для портфолио', // описание таксономии
    'public'                => true,
    'show_in_nav_menus'     => false, // равен аргументу public
    'show_ui'               => true, // равен аргументу public
    'show_tagcloud'         => false, // равен аргументу show_ui
    'hierarchical'          => true,
    'rewrite'               => array('slug'=>'works', 'hierarchical'=>false, 'with_front'=>false, 'feed'=>false ),
    'show_admin_column'     => true, // Позволить или нет авто-создание колонки таксономии в таблице ассоциированного типа записи. (с версии 3.5)
    ) );

  // тип записи - вопросы - works
  register_post_type('works', array(
    'label'               => 'Наши работы',
    'labels'              => array(
            'name' => 'Наши работы',
            'singular_name' => 'Работа',
            'add_new' => 'Добавить работу',
            'add_new_item' => 'Добавить новую работу',
            'edit_item' => 'Редактировать работу',
            'new_item' => 'Новая работа',
            'all_items' => 'Все работы',
            'search_items' => 'Искать',
            'not_found' =>  'Работа не найдена.',
            'not_found_in_trash' => 'В корзине нет работ.',
            'menu_name' => 'Работы'
        ),
        'menu_icon' => 'dashicons-portfolio',
    'description'         => '',
    'public'              => true,
    'publicly_queryable'  => true,
    'show_ui'             => true,
        'show_in_rest'        => false,
        'menu_position' => 5,
    'rest_base'           => '',
    'show_in_menu'        => true,
    'exclude_from_search' => false,
    'capability_type'     => 'post',
    'map_meta_cap'        => true,
    'hierarchical'        => false,
    'rewrite'             => array( 'slug'=>'works/%workscat%', 'with_front'=>false, 'pages'=>false, 'feeds'=>false, 'feed'=>false ),
    'has_archive'         => true,
    'query_var'           => true,
    'supports'            => array( 'title', 'editor' ),
    'taxonomies'          => array( 'workscat' ),
  ) );

}


I've tried like this, but it doesn't output anything
<?php  
$terms = get_terms( array(
'taxonomy' => 'workscat',
'parent' => '10'
) );

 if ( !empty( $terms ) && !is_wp_error( $terms ) ){
     echo '<ul>';
     foreach ( $terms as $term ) {
       echo '<li>' . $term->name . '&nbsp;(' . $term->count . ')' . '</li>';
     }
     echo '</ul>';
 }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Orkhan Hasanli, 2020-07-06
@DELUX

Firstly, you can add
'hide_empty' => false,
otherwise empty categories are hidden by default and maybe because of this nothing is displayed
Secondly,

'taxonomy' => 'workscat',
'parent' => '10'

Do I understand correctly that you are displaying the child categories of the parent category with ID 10 from the workscat taxonomy? Are there child categories for category ID 10 and is there a category with ID 10 at all?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question