【问题标题】:How to show the category name of my custom post type on single.php wordpress如何在 single.php wordpress 上显示我的自定义帖子类型的类别名称
【发布时间】:2021-03-08 20:11:02
【问题描述】:

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

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

我在注册自定义帖子类型中的代码:

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

【问题讨论】:

    标签: php wordpress custom-post-type taxonomy


    【解决方案1】:

    您可以使用get_term_link()

    前:

    <?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>';
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-11
      • 2014-04-14
      • 2016-08-08
      • 1970-01-01
      相关资源
      最近更新 更多