繁体   English   中英

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

[英]Displaying Custom Post Type's taxonomy

我目前正在建立我的第一个wordpress主题,我想在其预览以及发布日期上显示自定义帖子类型的分类法(但仅显示名称, 而不显示link )。

我一直在环顾四周,但只发现我应该使用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 )
    )
);

谢谢你的帮助 !

您应该可以使用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;
?>

暂无
暂无

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

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