0

Делаю простой каталог на базе Custom Post Type

Создал кастомную запись с возможностью добавления рубрики

<?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 );

разобрался с выводом записей

на этой же странице появилось необходимость в сайдбаре вывести список рубрик

я добавил

wp_list_categories()

но он выводит все категории которые есть на сайте,

нужно вывести только рубрики этой кастомной записи

как решить подкиньте идею

2 ответа 2

1

У кастомных типов записей не категории, а таксономии. Получить и вывести их можно таким кодом:

$args = array(
    'object_type' => array( 'gallery' ),
);
$taxonomies = get_taxonomies( $args );
foreach( $taxonomies as $taxonomy ) {
    echo '<p>'. $taxonomy. '</p>';
}
1

Решил задачу примерно так в function.php создал кастомную таксономию для этой записи

function gallery_categories() {
    $field_args = array(
        'labels' => array(
        'name'              => 'Галерея', 'taxonomy general name',
        'singular_name'     => 'Галерея', 'taxonomy singular name',
        'search_items'      => 'Search Categories',
        'all_items'         => 'Все',
        'parent_item'       => 'Parent Галерея',
        'parent_item_colon' => 'Parent Category:',
        'edit_item'         => 'Edit Category',
        'update_item'       => 'Update Category',
        'add_new_item'      => 'Добавить новую',
        'new_item_name'     => 'Новая',
        'menu_name'         => 'Рубрика Галереи',
        ),
        'hierarchical' => true
    );
    register_taxonomy( 'gallery_categories', 'gallery', $field_args );
}
add_action( 'init', 'gallery_categories', 0 );

и в шаблоне добавил

<?php   $terms = get_terms( 'gallery_categories' );
        if( $terms && ! is_wp_error($terms) ){
            foreach( $terms as $term ){
           echo '<ul class="product-categories">';	
        echo '<li><a href="' . get_term_link( $term ) . '">'. $term->name .'</a></li>';
       echo '</ul>';   
        }
    }
   ?>

список вывелся

введите сюда описание изображения

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.