简体   繁体   English

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

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

I would like to show the category name on my template single.php.我想在我的模板 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.我尝试使用 the_category 和 get_the_category,但没有用。 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()您可以使用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>';
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM