custom post types - How to display hierarchical and non-hierarchical taxonomies?

I have created custom post type and 4 taxonomies. First, second and third taxonomy is hierachical, the last is not. Is t

I have created custom post type and 4 taxonomies. First, second and third taxonomy is hierachical, the last is not. Is there any possibility to display separetly hierarchical and non-hierarchical taxonomies, without using their names?

For better understand: CPT has 4 taxonomies. 1, 2 and 3 are hierarchical, 4 taxonomy is non-hierachical. Now in span#one I want to display all hierarchical terms and in span#two all non-hierarchical terms of specific post.

How can I do that?

I have created custom post type and 4 taxonomies. First, second and third taxonomy is hierachical, the last is not. Is there any possibility to display separetly hierarchical and non-hierarchical taxonomies, without using their names?

For better understand: CPT has 4 taxonomies. 1, 2 and 3 are hierarchical, 4 taxonomy is non-hierachical. Now in span#one I want to display all hierarchical terms and in span#two all non-hierarchical terms of specific post.

How can I do that?

Share Improve this question edited May 15, 2019 at 8:53 Piotr Milecki asked May 15, 2019 at 7:51 Piotr MileckiPiotr Milecki 253 bronze badges 4
  • the OP is not clear about what he want to achieve please explain the things more clearly – siddhesh Commented May 15, 2019 at 8:21
  • Do you want to display all terms from these taxonomies or only the terms assigned to current post? – Krzysiek Dróżdż Commented May 15, 2019 at 8:34
  • What are you having trouble with, specifically? Terms assigned to a post need to be output separately for each taxonomy anyway, so you'd just put them in different elements. Are you just having trouble listing terms at all? – Jacob Peattie Commented May 15, 2019 at 8:38
  • @KrzysiekDróżdż take a look above I updated my post. I just want to separate hierarchical and non-hierarchical terms of specific post. – Piotr Milecki Commented May 15, 2019 at 8:56
Add a comment  | 

1 Answer 1

Reset to default 1

At the beginning, get taxonomies registered for a given type of post with get_object_taxonomies(). Then check which ones are hierarchical, e.g. using is_taxonomy_hierarchical() conditional tag. The last step is to display the lists.

// current post ID
$post_id = get_the_ID();
// taxonomies registered for this type of posts
$taxonomies = get_object_taxonomies( get_post_type() );

$taxs_tree = [];   // hierarchical taxonomies
$taxs_flat = [];   // non-hierarchical taxonomies
foreach ( $taxonomies as $tax )
{
    if ( is_taxonomy_hierarchical( $tax ) )
        $taxs_tree[] = $tax;
    else
        $taxs_flat[] = $tax;
}

Get terms from each taxonomy and display them.

// get terms as links
$terms_flat = $terms_tree = [];
foreach ( $taxs_tree as $tax ) {
    $terms_tree[] = get_the_term_list( $post_id, $tax );
}
foreach ( $taxs_flat as $tax ) {
    $terms_flat[] = get_the_term_list( $post_id, $tax );
}

echo '<span id="one">';
foreach ( $terms_tree as $links ) {
   echo $links;
}
echo '</span>';

Or:

//
// terms assigned to post, ordered by name, from all hierarchical taxonomies
$args = [
   'taxonomy'   => $taxs_tree,   // here you can pass array of taxonomies
   'object_ids' => get_the_ID(), // get only terms assigned to this post (current post)
   //'orderby.  => 'name',       // default
];
$terms_tree = get_terms( $args ); // array of WP_term objects

//
// terms assigned to post, ordered by name, from all non-hierarchical taxonomies
$args['taxonomy'] = $taxs_flat;
$terms_flat = get_terms( $args ); // array of WP_term objects

//
// display 
echo '<span id="one">';
foreach ( $terms_tree as $term ) {
    $link = get_term_link( $term );
    if ( is_wp_error( $link ) ) {
        continue;
    }
    echo '<a href="' . esc_url( $link ) . '" rel="tag">' . $term->name . '</a> ';
}
echo '</span>';

// the same with $terms_flat

The second solution (with get_terms) sort terms from all hierarchical taxonomies by name, while the first one (with get_the_term_list) sort terms from each hierarchical taxonomy separately.


References:

  • get_terms() - function, parameters
  • get_term_link()
  • get_the_term_list()

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

相关推荐

  • custom post types - How to display hierarchical and non-hierarchical taxonomies?

    I have created custom post type and 4 taxonomies. First, second and third taxonomy is hierachical, the last is not. Is t

    2小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信