I want to get/show only available tags under custom taxonomy. Here's my code;
$tax = $wp_query->get_queried_object(); // print current taxonomy
$args = array(
'post_type' => 'types', // CPT
'taxonomy' => $tax->slug, //get current taxonomy page
'orderby' => 'name',
'order' => 'DESC',
'hide_empty' => false
);
$tags = get_tags( $args );
foreach ( $tags as $tag ) {
echo '<li>'.$tag->name.'</li>';
}
My problem is in other page which don't have the tags under the taxonomy always shows. Any suggestion to this?
I want to get/show only available tags under custom taxonomy. Here's my code;
$tax = $wp_query->get_queried_object(); // print current taxonomy
$args = array(
'post_type' => 'types', // CPT
'taxonomy' => $tax->slug, //get current taxonomy page
'orderby' => 'name',
'order' => 'DESC',
'hide_empty' => false
);
$tags = get_tags( $args );
foreach ( $tags as $tag ) {
echo '<li>'.$tag->name.'</li>';
}
My problem is in other page which don't have the tags under the taxonomy always shows. Any suggestion to this?
Share Improve this question edited Oct 15, 2019 at 1:09 mrrsb asked Oct 10, 2019 at 7:52 mrrsbmrrsb 1012 bronze badges2 Answers
Reset to default 0You can use the following code to get all tags in a taxonomy.
<?php
$tags = get_terms(
array(
'taxonomy' => 'your_taxonomy',
)
);
foreach( $tags as $tag ) {
echo $tag->name;
}
You can get the tags of current post using:
$tax = $wp_query->get_queried_object(); // print current taxonomy
$args = array(
'orderby' => 'name',
'order' => 'DESC',
);
if( 'types' == get_post_type( get_the_ID() ) ) {
$tags = wp_get_object_terms( get_the_ID(), $tax->slug, );
if ( ! empty( $tags ) ) {
if ( ! is_wp_error( $tags ) ) {
foreach( $tags as $tag ) {
echo '<li>'.esc_html( $tag->name ).'</li>';
}
}
}
}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745080431a4610091.html
评论列表(0条)