2017.7.12 wordpress 自定义文章类型返回父级分类/输出父级名字(不带连接)

返回父级分类
在function里加入
function wpdocs_custom_taxonomies_terms_links() {
// Get post by post ID.
$post = get_post( $post->ID );
// Get post type by post.
$post_type = $post->post_type;
// Get post type taxonomies.
$taxonomies = get_object_taxonomies( $post_type, 'objects' );
$out = array();
foreach ( $taxonomies as $taxonomy_slug => $taxonomy ) {
// Get the terms related to post.
$terms = get_the_terms( $post->ID, $taxonomy_slug );
if ( !empty( $terms ) ) {
foreach ( $terms as $term ) {
$out[] = sprintf( '<a class="cont-all" href="%1$s">返回</a>',
esc_url( get_term_link( $term->slug, $taxonomy_slug ) ),
esc_html( $term->name )
);
}
}
}
return implode( '', $out );
}
然后在自定义的模板需要的地方
<?php wp_reset_query(); $output= wpdocs_custom_taxonomies_terms_links();?>
输出父级名字(不带连接)
在function里加入
function wpdocs_custom_taxonomies_terms_name() {
// Get post by post ID.
$post = get_post( $post->ID );
// Get post type by post.
$post_type = $post->post_type;
// Get post type taxonomies.
$taxonomies = get_object_taxonomies( $post_type, 'objects' );
$out = array();
foreach ( $taxonomies as $taxonomy_slug => $taxonomy ) {
// Get the terms related to post.
$terms = get_the_terms( $post->ID, $taxonomy_slug );
if ( !empty( $terms ) ) {
foreach ( $terms as $term ) {
$out[] = sprintf( '%1$s',
esc_html( $term->name )
);
}
}
}
return implode( '', $out );
}
然后在自定义的模板需要的地方
<?php wp_reset_query(); $output= wpdocs_custom_taxonomies_terms_name();?>
相关文章