categories - Display Custom Category (taxonomy) Name in Custom Post Type

I have a custom post type called results. I also have categories for that specific post type. My goal is to echo out the

I have a custom post type called results. I also have categories for that specific post type.

My goal is to echo out the category name of the post in the custom post type as set it as an HTML class.

Here is the code that sets up my custom post type and custom taxonomy:

// Create custom post type
function create_posttype() {
    register_post_type( 'Results',
        array(
            'labels' => array(
                'name' => __( 'Results' ),
                'singular_name' => __( 'Results' )
            ),
            'public' => true,
            'has_archive' => true,
            'rewrite' => array('slug' => 'results'),
            'taxonomies'  => array( 'results', 'result-category' ),
        )
    );
}
add_action( 'init', 'create_posttype' );

//Create category for specific post type
function tr_create_my_taxonomy() {
    register_taxonomy(
        'results-categories',
        'results',
        array(
            'label' => __( 'Result Categories' ),
            'rewrite' => array( 'slug' => 'result-category' ),
            'hierarchical' => true,
        )
    );
}
add_action( 'init', 'tr_create_my_taxonomy' );

Heres how im displaying the custom post type on one of my pages:

<?php
$query = new WP_Query( array( 'post_type' => 'Results',  'posts_per_page' => -1 ) );
if ( $query->have_posts() ) : ?>
    <?php while ( $query->have_posts() ) : $query->the_post(); ?>
        <div class="result-item">
            <div class="<?php //GOAL: code to display the category ?>"></div>
        </div>
    <?php endwhile; wp_reset_postdata(); ?>
<?php else : ?>
<?php endif; ?>

Thanks!

I have a custom post type called results. I also have categories for that specific post type.

My goal is to echo out the category name of the post in the custom post type as set it as an HTML class.

Here is the code that sets up my custom post type and custom taxonomy:

// Create custom post type
function create_posttype() {
    register_post_type( 'Results',
        array(
            'labels' => array(
                'name' => __( 'Results' ),
                'singular_name' => __( 'Results' )
            ),
            'public' => true,
            'has_archive' => true,
            'rewrite' => array('slug' => 'results'),
            'taxonomies'  => array( 'results', 'result-category' ),
        )
    );
}
add_action( 'init', 'create_posttype' );

//Create category for specific post type
function tr_create_my_taxonomy() {
    register_taxonomy(
        'results-categories',
        'results',
        array(
            'label' => __( 'Result Categories' ),
            'rewrite' => array( 'slug' => 'result-category' ),
            'hierarchical' => true,
        )
    );
}
add_action( 'init', 'tr_create_my_taxonomy' );

Heres how im displaying the custom post type on one of my pages:

<?php
$query = new WP_Query( array( 'post_type' => 'Results',  'posts_per_page' => -1 ) );
if ( $query->have_posts() ) : ?>
    <?php while ( $query->have_posts() ) : $query->the_post(); ?>
        <div class="result-item">
            <div class="<?php //GOAL: code to display the category ?>"></div>
        </div>
    <?php endwhile; wp_reset_postdata(); ?>
<?php else : ?>
<?php endif; ?>

Thanks!

Share Improve this question edited Aug 3, 2017 at 20:35 cup_of asked Aug 3, 2017 at 20:13 cup_ofcup_of 2191 gold badge4 silver badges14 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1
<?php  foreach((get_the_category()) as $category) { ?>
      <a href="<?php echo $category->category_nicename . ' '; ?>"><?php echo $category->category_nicename . ' '; ?></a>
<?php } ?>

insert above code here //GOAL: code to display the category//

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744933860a4601900.html

相关推荐

  • categories - Display Custom Category (taxonomy) Name in Custom Post Type

    I have a custom post type called results. I also have categories for that specific post type. My goal is to echo out the

    1天前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信