如何在 single.php wordpress 上显示我的自定义 post 类型的类别名称

How to show the category name of my custom post type on single.php wordpress

我想在我的模板上显示类别名称 single.php。我实现了自定义 post 类型和自定义分类法。

我尝试使用the_category和get_the_category,但是没有用。我觉得我做错了什么。

我的注册自定义代码 post 类型:

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

我在注册分类法中的代码:

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

您可以使用 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>';
}