admin管理员组文章数量:1406942
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 badges1 Answer
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//
本文标签: categoriesDisplay Custom Category (taxonomy) Name in Custom Post Type
版权声明:本文标题:categories - Display Custom Category (taxonomy) Name in Custom Post Type 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744933860a2633062.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论