douao2000 2018-06-19 14:13
浏览 47
已采纳

显示自定义帖子类型的分类

I'm currently building my first wordpress theme and I want to display a custom post type's taxonomy (but only the name, not the link) on its preview as well as the date it was published.

I've been looking around but only found that I should use the get_terms() functions but than displays all taxonomies, and I want to display the selected category for my post.

Please keep in mind that I'm only a beginner at this so you'll have to be pretty clear :)

Here is the loop :

<?php $loop = new WP_Query( array( 'post_type' => 'projets', 'posts_per_page' => '1' ) ); ?>
    <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
        <?php the_title() ?>
        <span>**SHOW CREATION DATE AND CATEGORY HERE**</span>
    <?php endwhile; wp_reset_query(); ?>

And here is the code for custom post type and taxonomy

// Custom post types FOLIO
function custom_post_type() {

// Set UI labels for Custom Post Type
$labels = array(
    'name'                => _x( 'Projets', 'Post Type General Name', 'connivence' ),
    'singular_name'       => _x( 'Projet', 'Post Type Singular Name', 'connivence' ),
    'menu_name'           => __( 'Projets', 'connivence' ),
    'parent_item_colon'   => __( 'Projet parent ', 'connivence' ),
    'all_items'           => __( 'Tous les projets', 'connivence' ),
    'view_item'           => __( 'Voir', 'connivence' ),
    'add_new_item'        => __( 'Ajouter un projet', 'connivence' ),
    'add_new'             => __( 'Ajouter', 'connivence' ),
    'edit_item'           => __( 'Editer', 'connivence' ),
    'update_item'         => __( 'Mettre à jour', 'connivence' ),
    'search_items'        => __( 'Recherche', 'connivence' ),
    'not_found'           => __( 'Not Found', 'connivence' ),
    'not_found_in_trash'  => __( 'Not found in Trash', 'connivence' ),
);

// Set other options for Custom Post Type
$args = array(
    'label'               => __( 'Projets', 'connivence' ),
    'description'         => __( 'Portfolio et projets', 'connivence' ),
    'labels'              => $labels,
    'supports'            => array( 'title', 'editor', 'thumbnail', 'revisions', 'custom-fields', ),
    'hierarchical'        => false,
    'public'              => true,
    'show_ui'             => true,
    'show_in_menu'        => true,
    'show_in_nav_menus'   => true,
    'show_in_admin_bar'   => true,
    'menu_position'       => 5,
    'can_export'          => true,
    'has_archive'         => true,
    'exclude_from_search' => false,
    'publicly_queryable'  => true,
    'capability_type'     => 'page',
    'menu_icon'           => 'dashicons-welcome-learn-more',
);

// Registering your Custom Post Type
register_post_type( 'Projets', $args );

// Registering your Custom Taxonomy
register_taxonomy( 'categories-projets', array('projets'), array(
    'hierarchical' => true, 
    'label' => 'Catégories', 
    'singular_label' => 'Catégorie', 
    'rewrite' => array( 'slug' => 'projets', 'with_front'=> false )
    )
);

Thank you for you help !

  • 写回答

1条回答 默认 最新

  • dongtaogou6226 2018-06-19 16:06
    关注

    You should be able to use get_the_terms() to do this.

    <?php
    $loop = new WP_Query( array( 'post_type' => 'projets', 'posts_per_page' => '1' ) );
    
    if ( $loop->have_posts() ) :
    
        while ( $loop->have_posts() ) : $loop->the_post();
    
            // get all of the terms for this post, with the taxonomy of categories-projets.
            $terms = get_the_terms( $post->ID, 'categories-projets' );
            the_title();
    
            // create the span element, and write out the date this post was created.
            echo "<span>" . the_date();
    
            foreach ( $terms as $term ) {
                echo $term->name;
            }
    
            echo "</span>";
    
    
        endwhile;
        wp_reset_query();
    
    endif;
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多
  • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
  • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000
  • ¥30 ppOCRLabel导出识别结果失败