How to get list custom taxonomy terms for specific taxonomy
-
I’d like to return the list of taxonomy terms associated with a custom taxonomy. The function
wp_list_categories
does not seem to work even if I specifiy the database ID for the custom taxonomy term parent.
-
You will have to use a tag cloud. This line will display them in an unordered list:
wp_tag_cloud( array( 'taxonomy' => 'name_of_your_taxonomy', format => 'list' ) );
That’s the trick. I’m surprised this hasn’t been written into another function yet, or as part of
wp_list_categories
. Oh to be a php ninja!Now the trouble is getting an “active” or “current” class in there when you’re on the taxonomy tag’s page. Any ideas?
I think that the function you need is called get_terms()
I think that the function you need is called get_terms()
Indeed,
get_terms()
also uses caching to, so it should be faster long term then any category or tag equivalents.
https://codex.www.ads-software.com/Function_Reference/get_termsThere’s also.
https://codex.www.ads-software.com/Function_Reference/get_the_term_listhow about using it in post list panel?
Can you clarify the question.
@t31os_ see this screenshot : https://dl.dropbox.com/u/2317543/Screen%20shot%202010-06-07%20at%202.54.14%20PM.png
i would like to display custom categories and tags in the panel like the normal post list.
I’m not sure what you mean, custom post types and custom taxonomies appear in my admin menu.
Assuming you’re using 3.0 you should have menu items for them to…
https://t31os.files.wordpress.com/2010/06/wp3menu.png
The taxonomy page works just like the regular post tag and category pages in WordPress, do you do not see yours there?
Is your taxonomy hierarchal? (i’m sure i had issues with hierarchal taxonomies appearing early 3.0)
Did you set show_ui to true when registering the taxonomy??i mean on your books list or edit page. admin menu just work fine.
i want to display categories and tags like this :
https://dl.dropbox.com/u/2317543/Screen%20shot%202010-06-08%20at%203.03.56%20AM.png
they are missing here :
https://dl.dropbox.com/u/2317543/Screen%20shot%202010-06-07%20at%202.54.14%20PM.png
If you setup your custom post type to use the built in categories and/or post tags you can have the columns show up by adding the taxonomies parameter when you register the post type..
Example of my book post type.
register_post_type( 'book', array( 'labels' => array( 'name' => 'Books', 'singular_name' => 'Book', 'add_new' => 'Add New', 'add_new_item' => 'Add New Book', 'edit_item' => 'Edit book', 'edit' => 'Edit', 'new_item' => 'New Book', 'view_item' => 'View Book', 'search_items' => 'Search Books', 'not_found' => 'No books found', 'not_found_in_trash' => 'No books found in Trash', 'view' => 'View Book' ), 'public' => true, 'capability_type' => 'post', 'hierarchical' => false, 'rewrite' => true, 'query_var' => true, 'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'custom-fields', 'comments' ), 'taxonomies' => array('category','post_tag') ) );
The columns will then appear as they would for posts… ??
how about to use custom categories and/or post tags ?
Well no, but then you’d not see custom taxonomy columns for regular post types either if i remember correctly..
However, with a few hooks you can add the extra columns in(same way you would for posts).. just off to bed shortly, but i’ll see about providing an example for you tomorrow.. (can’t think code when i’m tired).
Pretty sure though, that it’s possible with the right hooks … ??
I decided i’d try and dig out some guides to save me having to write it all out, but it’s look like you’ve seen this one already at least.. ??
deluxeblogtips.com/2010/05/add-custom-column.html
There’s also this one to..
scompt.com/blog/archives/2007/10/20/adding-custom-columns-to-the-wordpress-manage-posts-screen
For custom post types, you just replace
manage_posts_column
withmanage_{post_type}_posts_column
.Here’s a tested example anyway… using my books post type with the book_category taxonomy i have registered for that post type.
function add_new_columns($defaults) { $defaults['book_cats'] = __('Book Categories'); return $defaults; } function add_column_data( $column_name, $post_id ) { if( $column_name == 'book_cats' ) { $_taxonomy = 'book_category'; $terms = get_the_terms( $post_id, $_taxonomy ); if ( !empty( $terms ) ) { $out = array(); foreach ( $terms as $c ) $out[] = "<a href='edit-tags.php?action=edit&taxonomy=$_taxonomy&post_type=book&tag_ID={$c->term_id}'> " . esc_html(sanitize_term_field('name', $c->name, $c->term_id, 'category', 'display')) . "</a>"; echo join( ', ', $out ); } else { _e('Uncategorized'); } } } add_filter( 'manage_book_posts_columns', 'add_new_columns' ); add_action( 'manage_posts_custom_column', 'add_column_data', 10, 2 );
Copied most of that code from WordPress, making minor adjustments where necessary.
@t31os_ very much thanks to you. managed to get it done with your help. really happy to share my code to whoever want to have custom categories and tags to be displayed like normal post list.
function product_columns($posts_columns) { $posts_columns = array(); $posts_columns['cb'] = '<input type="checkbox" />'; $posts_columns['title'] = _x('Title', 'column name'); $posts_columns['author'] = __('Author'); if ( empty($post_type) || is_object_in_taxonomy($post_type, 'category') ) $posts_columns['product_categories'] = __('Categories'); if ( empty($post_type) || is_object_in_taxonomy($post_type, 'post_tag') ) $posts_columns['product_tags'] = __('Tags'); $post_status = !empty($_REQUEST['post_status']) ? $_REQUEST['post_status'] : 'all'; if ( !in_array( $post_status, array('pending', 'draft', 'future') ) && ( empty($post_type) || post_type_supports($post_type, 'comments') ) ) $posts_columns['comments'] = '<div class="vers"><img alt="Comments" src="' . esc_url( admin_url( 'images/comment-grey-bubble.png' ) ) . '" /></div>'; $posts_columns['date'] = __('Date'); return $posts_columns; } function product_data_columns( $column_name) { switch ($column_name) { case 'product_categories': $_taxonomy = 'product_category'; $categories = get_the_terms( $post_id, $_taxonomy ); if ( !empty( $categories ) ) { $out = array(); foreach ( $categories as $c ) $out[] = "<a href='edit.php?product_category=$c->slug&post_type=product'> " . esc_html(sanitize_term_field('name', $c->name, $c->term_id, 'category', 'display')) . "</a>"; echo join( ', ', $out ); } else { _e('Uncategorized'); } break; case 'product_tags': $_taxonomy = 'product_tag'; $tags = get_the_terms( $post_id, $_taxonomy ); if ( !empty( $tags ) ) { $out = array(); foreach ( $tags as $c ) $out[] = "<a href='edit.php?product_tag=$c->slug&post_type=product'> " . esc_html(sanitize_term_field('name', $c->name, $c->term_id, 'post_type', 'display')) . "</a>"; echo join( ', ', $out ); } else { _e(' No Tags'); } break; } } add_filter( 'manage_product_posts_columns', 'product_columns' ); add_action( 'manage_posts_custom_column', 'product_data_columns', 10, 2 );
just like t31os_ code, most of it i copied from template.php and make minor adjustment to reflect my custom taxonomies.
- The topic ‘How to get list custom taxonomy terms for specific taxonomy’ is closed to new replies.