WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] How to get list custom taxonomy terms for specific taxonomy (21 posts)

  1. stunnaboi
    Member
    Posted 4 years ago #

    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.

  2. kjeft
    Member
    Posted 4 years ago #

    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' ) );

  3. stunnaboi
    Member
    Posted 4 years ago #

    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!

  4. stunnaboi
    Member
    Posted 3 years ago #

    Now the trouble is getting an "active" or "current" class in there when you're on the taxonomy tag's page. Any ideas?

  5. ReflectionMedia
    Member
    Posted 3 years ago #

    I think that the function you need is called get_terms()

  6. Mark / t31os
    Moderator
    Posted 3 years ago #

    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.
    http://codex.wordpress.org/Function_Reference/get_terms

    There's also.
    http://codex.wordpress.org/Function_Reference/get_the_term_list

  7. Od3n
    Member
    Posted 3 years ago #

    how about using it in post list panel?

  8. Mark / t31os
    Moderator
    Posted 3 years ago #

    Can you clarify the question.

  9. Od3n
    Member
    Posted 3 years ago #

    @t31os_ see this screenshot : http://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.

  10. Mark / t31os
    Moderator
    Posted 3 years ago #

    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...

    http://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??

  11. Od3n
    Member
    Posted 3 years ago #

    i mean on your books list or edit page. admin menu just work fine.

    i want to display categories and tags like this :

    http://dl.dropbox.com/u/2317543/Screen%20shot%202010-06-08%20at%203.03.56%20AM.png

    they are missing here :

    http://dl.dropbox.com/u/2317543/Screen%20shot%202010-06-07%20at%202.54.14%20PM.png

  12. Mark / t31os
    Moderator
    Posted 3 years ago #

    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... :)

  13. Od3n
    Member
    Posted 3 years ago #

    how about to use custom categories and/or post tags ?

  14. Mark / t31os
    Moderator
    Posted 3 years ago #

    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 ... :)

  15. Mark / t31os
    Moderator
    Posted 3 years ago #

    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 with manage_{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.

  16. Od3n
    Member
    Posted 3 years ago #

    @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.

  17. bennst
    Member
    Posted 3 years ago #

    First off thank you for posting this code. But I'm afaid I'm having a few issues.

    The code works perfectly, execpt for the fact that no information is being displayed in the column until I have clicked 'Quick Edit' for each post and then updated the information. This appears to magically bring data into the new columns but disappears again when the page is refreshed.

    Anyone got any idea where I could've gone wrong? Almost like the code needs to run on_load as apposed to when the post is updated - if that makes sense?

    Thanks!

  18. Niraj Chauhan
    Member
    Posted 3 years ago #

    how can i display custom taxonomies on selected pages?
    i mean i have custom taxonomies location,duration & courses
    i have created three pages where i want to display the terms of these custom taxonomies,
    i have displayed it on my posts by using function
    <?php echo do_shortcode("[terms]"); ?>

    but same thing i want to make display on my pages also,
    how can i do that??????

  19. FayssalF
    Member
    Posted 3 years ago #

    @Niraj - Try this:

    <?php echo get_the_term_list( $post->ID, 'location', '<p>Location  : ', ', ', '</p>' ) ?>
    <?php echo get_the_term_list( $post->ID, 'duration', '<p>Duration  : ', ', ', '</p>' ) ?>
    <?php echo get_the_term_list( $post->ID, 'courses', '<p>List of courses  : ', ', ', '</p>' ) ?>

    You might probably use it in your single-custom-post-type.php just around <?php the_content(); ?>

  20. ryansigg
    Member
    Posted 3 years ago #

    I'm having a similar problem as Bennst above; since this thread is marked "resolved" and only partially pertains to my problem, I've started a more specific one here: http://wordpress.org/support/topic/list-custom-taxonomies-in-custom-columns

  21. snails07
    Member
    Posted 3 years ago #

    Been looking for the same and just found this. Seemed to be the perfect solution for me and uses wp_list_categories .

    //list terms in a given taxonomy using wp_list_categories (also useful as a widget if using a PHP Code plugin)
    
    $taxonomy     = 'eventcategory';
    $orderby      = 'name';
    $show_count   = 0;      // 1 for yes, 0 for no
    $pad_counts   = 0;      // 1 for yes, 0 for no
    $hierarchical = 1;      // 1 for yes, 0 for no
    $title        = '';
    $empty        = 0;
    
    $args = array(
      'taxonomy'     => $taxonomy,
      'orderby'      => $orderby,
      'show_count'   => $show_count,
      'pad_counts'   => $pad_counts,
      'hierarchical' => $hierarchical,
      'title_li'     => $title,
      'hide_empty'   => $empty
    );
    
    <ul>
    <?php wp_list_categories( $args ); ?>
    </ul>

Topic Closed

This topic has been closed to new replies.

About this Topic