I would like to show the category name on my template single.php. I implemented custom post type and custom taxonomy.
I tried to use the_category and get_the_category, but it didn't work. I think I'm doing something wrong.
My code in register custom post type :
$gotex_args = array(
'labels' => array(
'name' => 'Dla domu',
'singular_name' => 'Dla domu',
'all_items' => 'Dla domu',
'add_new' => 'Dodaj nowy wpis',
'add_new_item' => 'Dodaj nowy wpis',
'edit_item' => 'Edytuj wpis',
'new_item' => 'Nowy wpis',
'view_item' => 'Zobacz wpis',
'search_items' => 'Szukaj w wpisach',
'not_found' => 'Nie znaleziono wpisu',
'not_found_in_trash' => 'Brak wpisów w koszu',
'parent_item_colod' => ''
),
'public' => true,
'public_queryable' => true,
'show_ui' => true,
'query_ver' => true,
'rewrite' => true,
'capability_type' => 'post',
'hierarchical' => false,
'menu_position' => 5,
'show_in_rest' => true,
'supports' => array(
'title', 'editor', 'thumbnail'
),
'has_archive' => true
);
register_post_type('dla-domu', $gotex_args);
My code in register taxonomy:
function dladomu_custom_taxonomy() {
$labels = array(
'name' => _x( 'Kategorie', 'Taxonomy General Name', 'text_domain' ),
'singular_name' => _x( 'Kategorie', 'Taxonomy Singular Name', 'text_domain' ),
'menu_name' => __( 'Kategorie', 'text_domain' ),
'all_items' => __( 'Wszystkie kategorie', 'text_domain' ),
'parent_item' => __( 'Parent Item', 'text_domain' ),
'parent_item_colon' => __( 'Parent Item:', 'text_domain' ),
'new_item_name' => __( 'Nazwa', 'text_domain' ),
'add_new_item' => __( 'Dodaj nową kategorię', 'text_domain' ),
'edit_item' => __( 'Edytuj kategorię', 'text_domain' ),
'update_item' => __( 'Aktualizuj kategorie', 'text_domain' ),
'view_item' => __( 'Wyświetl kategorie', 'text_domain' ),
'separate_items_with_commas' => __( 'Oddziel kategorie przecinkami', 'text_domain' ),
'add_or_remove_items' => __( 'Dodaj lub usuń kategorię', 'text_domain' ),
'choose_from_most_used' => __( 'Wybierz jedną z najczęściej używanych', 'text_domain' ),
'popular_items' => __( 'Popularne kategorie', 'text_domain' ),
'search_items' => __( 'Szukaj kategorii', 'text_domain' ),
'not_found' => __( 'Nie znaleziono', 'text_domain' ),
'no_terms' => __( 'Brak kategorii', 'text_domain' ),
'items_list' => __( 'Lista kategorii', 'text_domain' ),
'items_list_navigation' => __( 'Nawigacja kategorii', 'text_domain' ),
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'show_in_rest' => true,
'show_tagcloud' => true,
);
register_taxonomy( 'kategorie_dladomu', array( 'dla-domu' ), $args );
}
add_action( 'init', 'dladomu_custom_taxonomy', 0 );
You can using get_term_link()
EX:
<?php
$terms = get_the_terms( get_the_ID(), 'kategorie_dladomu' );
if ( $terms && ! is_wp_error( $terms ) ) {
echo '<ul>';
foreach ( $terms as $term ) {
echo '<li><a href="'. get_term_link($term) .'">'. $term->name .'</a></li>';
}
echo '<ul>';
}