1
votes

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

1 Answers

2
votes

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