Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

This link tells me how to display categories assigned to a post.

How would I modify this code to display the terms from a custom taxonomy assigned to a post in hierarchical order?

share|improve this question

1 Answer 1

It depends of what you are trying to do exactly. But if you want the same functionality of the question you linked, you can use the same function but passing your custom taxonomy to the function. Here all the arguments you can pass to wp_list_categories() and is defulat values:

<?php $args = array(
     'show_option_all'    => '',
         //Possible values of orderby: ID, name, slug, count, term_group 
     'orderby'            => 'name',
     'order'              => 'ASC',
     'style'              => 'list',
     'show_count'         => 0,
     'hide_empty'         => 1,
     'use_desc_for_title' => 1,
     'child_of'           => 0,
     'feed'               => '',
     'feed_type'          => '',
     'feed_image'         => '',
     'exclude'            => '',
     'exclude_tree'       => '',
     'include'            => '',
     'hierarchical'       => 1,
     'title_li'           => __( 'Categories' ),
     'show_option_none'   => __('No categories'),
     'number'             => null,
     'echo'               => 1,
     'depth'              => 0,
     'current_category'   => 0,
     'pad_counts'         => 0,
          //Change this with your custom taxonomy
     'taxonomy'           => 'your_custom_taxonomy',
      'walker'             => null
);
wp_list_categories($args);
?>

The above code will generate a list in hierarchical order with all the terms from the specified taxonomy, not only the associated to the current post. But we can limit the included terms using the inlcude argument:

<?php
global $post;
$taxonomy = 'your_taxonomy';

// get the term IDs assigned to post.
$post_terms = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );

if ( !empty( $post_terms ) && !is_wp_error( $post_terms ) ) {

    $term_ids = implode( ',' , $post_terms );
    $args = array(
                'taxonomy'    => $taxonomy,
                'include'     => $term_ids,
                //Add more arguments if you need them with a different value from default
            );
    $terms = wp_list_categories($args);

    // display post categories
    echo  $terms;
}
?>
share|improve this answer
    
I've already tried that with no luck –  user1492779 Sep 17 '13 at 11:36
    
In my suggested code, have you changed 'your_custom_taxonomy' by the slug of your custom taxonomy? –  cybmeta Sep 17 '13 at 12:10
    
wordpress.org/support/topic/… –  user1492779 Sep 18 '13 at 5:09
    
Please, add code or new information directly into your question. –  cybmeta Sep 18 '13 at 7:53
    
Can you try the new suggestion I've made in my answer? –  cybmeta Sep 18 '13 at 8:05

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.