V
V
Viorel2018-10-14 18:59:19
CMS
Viorel, 2018-10-14 18:59:19

How to implement a list of categories of a custom post?

Making a simple catalog based on Custom Post Type
Created a custom post with the ability to add a category

<?php
// Register Custom Post Type
function custom_post_type_project() {

  $labels = array(
    'name'                  => 'Галерея',
    'singular_name'         => 'Gallery',
    'menu_name'             => 'Галерея',
    'add_new_item'          => 'Добавить новый',
    'add_new'               => 'Добавить новый',
    'new_item'              => 'Новая',
    'edit_item'             => 'Редактировать',
    'update_item'           => 'Обновить',
    'view_item'             => 'Просмотр',
    'view_items'            => 'Посмотреть все',
  );
  
  $rewrite = array(
    'slug'                  => 'gallery',
    'with_front'            => true,
    'pages'                 => false,
    'feeds'                 => false,
  );
  $args = array(
    'label'                 => 'Галерея',
    'labels'                => $labels,
    'supports'              => array( 'title', 'thumbnail', 'editor'),
    'taxonomies'            => array('category'),
    'hierarchical'          => false,
    'public'                => true,
    'show_ui'               => true,
    'show_in_menu'          => true,
    'menu_position'         => 7,
    'menu_icon'             => 'dashicons-format-image',
    'show_in_admin_bar'     => true,
    'show_in_nav_menus'     => true,
    'can_export'            => true,
    'has_archive'           => true,		
    'exclude_from_search'   => true,
    'publicly_queryable'    => true,
    'query_var'             => 'gallery',
    'rewrite'               => $rewrite,
  );
  register_post_type( 'gallery', $args );

}
add_action( 'init', 'custom_post_type_project', 0 );

figured out the output of posts
on the same page, there was a need to display a list of categories in the sidebar,
I added
wp_list_categories ()
but it displays all the categories that are on the site,
you need to display only the categories of this custom post
how to solve throw an idea

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Syomka Gavrilenko, 2018-10-14
@bushido2014

<?php
$terms = get_terms( array(
  'taxonomy'      => 'category',
  'orderby'       => 'name', 
  'hide_empty'    => false, 
  'count'         => false,
  'hierarchical'  => true, 
) );
$terms = wp_list_filter( $terms, array('parent'=>0) ); // Если надо вывести только рубрики верхнего уровня
?>
      <?php
        if( $terms && ! is_wp_error($terms) ){
          foreach( $terms as $term ){
            echo "<a href='". get_term_link( (int) $term->term_id, 'category' ) ."'>". $term->name ."</a>";
          }
        }
      ?>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question