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

时间:2018-06-19 14:13:10

标签: php wordpress loops custom-post-type

我当前正在构建我的第一个wordpress主题,并且希望在其预览以及发布日期上显示自定义帖子类型的分类法(但仅显示名称,不显示链接

我一直在环顾四周,但发现我应该使用 get_terms()函数,而不是显示所有分类法,并且我想显示帖子的选定类别。

请记住,我只是一个初学者,因此您必须非常清楚:)

这是循环:

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

这是自定义帖子类型和分类法的代码

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

谢谢您的帮助!

1 个答案:

答案 0 :(得分:1)

您应该可以使用get_the_terms()来做到这一点。

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