Wordpress custom taxonomies

June 2, 2010 | Wordpress tutorials | 9 Comments »

Wordpress custom taxonomy is a very powerful and useful feature, you can classify posts in many different ways, for example: movies, music, photos, artists, actors or anything else.
So how to create custom taxonomy? Well let’s look at the example below.
This code will register custom taxonomy:

<?php

//this code must be placed in functions.php file.

add_action( 'init', 'create_my_taxonomies', 0 );

function create_my_taxonomies() {
register_taxonomy( 'company', 'post', array( 'hierarchical' => false, 'label' => 'Company', 'query_var' => true, 'rewrite' => true ) );
register_taxonomy( 'my_taxonomy', 'post', array( 'hierarchical' => false, 'label' => 'My Taxonomy', 'query_var' => true, 'rewrite' => true ) );
}

?>
  • my_taxonomy – custom taxonomy name
  • My taxonomy – custom taxonomy label
  • hierarchical – can be used as true/false

False – non hierarchical tag type taxonomy, true – hierarchical category type taxonomy.
For now you must use “false”, because hierarchical custom taxonomies will be supported just in wordpress 3.0.

After you have made changes in functions.php file you can go to wordpress admin panel to check if everything works.

Custom taxonomy panels


Getting custom taxonomy terms on your page

Tagcloud

It’s very easy to make a tagcloud from taxonomy terms, just set the taxonomy argument.
You can place this code whatever you want.

<?php wp_tag_cloud( array( 'taxonomy' => 'my_taxonomy', 'number' => 45 ) ); ?>

List taxonomy terms for each post

<?php echo get_the_term_list( $post->ID, 'my_taxonomy', 'My Taxonomy: ', ', ', '' ); ?>

Custom taxonomy template hierarchy

  • taxonomy.php – for all custom taxonomies
  • taxonomy-{taxonomy-name}.php – template for some taxonomy
  • taxonomy-{taxonomy-name}-{term}.php – template for taxonomy term

9 Responses

  1. Wordpress is definitely the best blogging platform, no one can beat wordpress’.,

  2. Thank you for another informative blog. Where else could I get that kind of info written in such a perfect way? I have a project that I am just now working on, and I have been on the look out for such information.

  3. wordpress is simply the best blogging platfrom that you can use, it has lots of features *’.

  4. Hello sir

    i want to use wordpress nav menu function.
    i am using primary menu and added few sub menu in that menu using wordpress admin.

    Now i want to show those parent menu and their child menu in header dropdown of my website.

    I am using the function
    $menus = get_terms( ‘nav_menu’, array( ‘hide_empty’ => true ) );

    But i dont have any clue about achieving this functionality in header.
    My code development website is http://showprojects.net/projects/csrc/

    Thanks for support

  5. I discovered your blog web site on google and verify a few of your early posts. Proceed to keep up the superb operate. I simply additional up your RSS feed to my MSN Information Reader. Seeking forward to reading more from you afterward!…

  6. Thanh Almen says:

    hello!,I like your writing very much! share we communicate more about your post on AOL? I need an expert on this area to solve my problem.May be that’s you! Looking forward to see you.

  7. Does your website have a contact page? I’m having problems locating it but, I’d like to send you an email. I’ve got some suggestions for your blog you might be interested in hearing. Either way, great blog and I look forward to seeing it grow over time.

  8. Thanks for the info. Not wasting my time

  9. F*ckin’ remarkable things here. I am very happy to see your post. Thanks so much and i’m taking a look ahead to touch you. Will you kindly drop me a mail?

Leave a Reply