Try Tuts+ Premium, Get Cash Back!
Introducing WordPress 3 Custom Taxonomies

Introducing WordPress 3 Custom Taxonomies

Tutorial Details
  • Program: WordPress
  • Version: 3.0
  • Difficulty: Intermediate
  • Estimated Completion Time: 20 Minutes

WordPress 3 fills in a number of important gaps towards being a serious content management system. The easy-to-use custom taxonomies function gives site designers some powerful tools for building a good information architecture. Learn what taxonomies are, why they’re useful, and how to use them in today’s tutorial!


What is a Taxonomy?

Taxonomies are different methods for classifying things.

Taxonomies are different methods for classifying things. This tutorial uses an example of posts about different desktop computers, which can be classified by a number of distinct criteria, including:

  • Amount of RAM
  • Size of hard drive
  • Speed of CPU
  • Type of CPU
  • Operating system installed
  • and so forth…

A Brief History of WordPress Taxonomies

Categories

Category Example

Prior to version 2.3, WordPress had only one generic taxonomy, called Category, for Posts. This worked well for blogs, as you could create a top-level category called “Desktop Computers,” with a subcategory called “RAM,” which may have subcategories such as “Less than 1 GB,” “1 GB,” “2 GB to 4GB,” and so on. A second child category of “Desktop Computers” might be called “Operating System,” with subcategories such as “Windows XP,” “Mac OS,” “Red Hat,” “Ubuntu,” and so forth.

When a system allows you to have categories that can be divided into subcategories, we call it a hierarchical structure. The best you could do for a serious site architecture prior to WordPress version 2.3 was to create a large hierarchy of categories, where the top level categories represented large taxonomy groups.

Tags

Tags Example

Version 2.3 of WordPress added another type of taxonomy called Tags. While categories are usually thought out in advance, specific to the types of content on a site, tags provide a more freeform, impromptu method of classifying content.

For instance, when writing a Post about a particular desktop computer, tags allow the author to type one or more keywords such as “gaming,” “tivo,” “noisy fan,” and so forth. These keywords might not make sense as site-wide categories, but help provide some additional classification to a post. Site visitors could then easily find all the posts tagged with “noisy fan” later. The freeform nature of tags, however, doesn’t help us build a solid classification system around known values such as operating system types or CPU types. Tags are also one-dimensional, not allowing any hierarchical structure.

Single-Level Custom Taxonomies

Single-Level Custom Taxonomies

WordPress version 2.8 made it possible to add custom classification schemes with just a few changes to the code on your site. This allowed you to build a list of possible operating systems, separate from a list of possible RAM types, and so on. It did not, however, allow these custom taxonomies to be built in a hierarchy similar to the generic categories taxonomy.

Fully Hierarchical Custom Taxonomies

Fully Hierarchical Custom Taxonomies

Finally, WordPress version 3 gives us fully hierarchical custom taxonomies. Notice how the hierarchical nature allows us to simplify the Operating System taxonomy, for instance, by pushing all the different Windows variants under a “Windows” parent classification. This will allow visitors to see all posts classified with any Windows operating system, or allow them to be more specific and see only posts classified with Windows XP, for instance.


Creating a Custom Taxonomy

Editing your theme’s functions.php file

WordPress version 3 does not allow you to create custom taxonomies from the administration screen. To initially define your custom taxonomies without a plugin, you’ll need to add a little bit of code to your theme’s functions.php file. This isn’t too difficult — just follow my lead.

To add custom taxonomies, we need to edit the “functions.php” file found inside your theme directory. For instance, I’m using the default “twentyten” theme that comes with WordPress 3.0, and my WordPress installation is in a directory named “wp.” My functions.php file is then at:
[website_root]/wp/wp-content/themes/twentyten/functions.php.


Adding the Taxonomies in Code

We’ll stick with the Desktop Computer example, adding separate taxonomies for RAM, Hard Drive, and Operating System. At this point, we’re simply adding the taxonomies themselves, like empty containers. Fortunately, we can add and manage the different classifications, such as “Windows XP,” from the comfort of the admin dashboard.


Step 1 One Function to Create Them All

First, we need to build one function that creates all the taxonomies we need. We’ll call the function “build_taxonomies.” Let’s add this function to the bottom of the functions.php file.

function build_taxonomies() {
    // code will go here
}

Step 2 Defining the Taxonomies

Next, for each taxonomy we want to create, we need to call a particular WordPress function with the right parameters. Here’s the function, and its important parameters explained.

register_taxonomy(
	'internal_name',
	'object_type',
	array(
		'hierarchical' => {true|false},
		'label' => 'Human Readable Name',
		'query_var' => {true|false},
		'rewrite' => {true|false}
	)
);
  • internal_name: What will this taxonomy be called from inside WordPress, in the database and template files?
  • object_type: Which types of content can be classified with this taxonomy? Possible values are “post, page, link,” and then names of custom post types we’ll learn to create in a future tutorial.
  • Next comes an array of optional parameters. We’ll use the most important ones here in this tutorial, but a full list can be found on the Function Reference / register_taxonomy Codex page. The parameters we’ll use are:
  • hierarchical: If ‘true,’ this taxonomy has hierarchical abilities like WordPress Categories. If ‘false,’ this taxonomy behaves much like freeform Tags.
  • label: This is the human-readable name used in your site’s interface to label the taxonomy.
  • query_var: If ‘true,’ we’ll be able to ask WordPress for posts dependent upon the selections for this taxonomy. For example, we could search for all the posts where the operating system taxonomy has ‘Windows’ selected.
  • rewrite: If ‘true,’ WordPress will use friendly URL’s when viewing a page for this taxonomy. For example, a page listing all the posts with the “Windows” operating system selected would be represented by the following url: http://domain.com/operating_system/windows

Our entry specific to adding the Operating System taxonomy looks like so:

register_taxonomy( 'operating_system', 'post', array( 'hierarchical' => true, 'label' => 'Operating System', 'query_var' => true, 'rewrite' => true ) );

Go ahead and add that to your “build_taxonomies” function.

More information:

“register_taxonomy” is further defined within the WordPress codex.


Step 3 Calling the Taxonomy-Creating Function

We need to add one more line to the “functions.php” file so our “build_taxonomies” function will actually be executed. We’ll “hook” the “build_taxonomies” function to the “init” event by adding the following code:

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

You can add this line anywhere, but I generally add it above the function we’re calling, so it would look like this:

// Custom Taxonomy Code
add_action( 'init', 'build_taxonomies', 0 );

function build_taxonomies() {
    register_taxonomy( 'operating_system', 'post', array( 'hierarchical' => true, 'label' => 'Operating System', 'query_var' => true, 'rewrite' => true ) );
}

More information:

Learn more about add_action.


Adding Classifications to the New Taxonomy

Viewing taxonomy classifications

Once you’ve added the “Operating System” taxonomy to the “functions.php” file correctly, it should show up as a new item in the “Posts” panel of your dashboard. Click the taxonomy’s name to add and edit the classifications you want to include.

Adding taxonomy classifications

Now, you can add and edit Operating Systems just as you would add generic Categories.


Adding More Taxonomies

If you want to add the “RAM” and “Hard Drive” taxonomies to follow along with the example, just add the following to your functions.php file:

register_taxonomy( 'ram', 'post', array( 'hierarchical' => true, 'label' => 'RAM', 'query_var' => true, 'rewrite' => true ) );
register_taxonomy( 'hard_drive', 'post', array( 'hierarchical' => true, 'label' => 'Hard Drive', 'query_var' => true, 'rewrite' => true ) );

Once finished, the changed section of your functions.php file will look something like this:

// Custom Taxonomy Code
add_action( 'init', 'build_taxonomies', 0 );

function build_taxonomies() {
register_taxonomy( 'operating_system', 'post', array( 'hierarchical' => true, 'label' => 'Operating System', 'query_var' => true, 'rewrite' => true ) );
register_taxonomy( 'ram', 'post', array( 'hierarchical' => true, 'label' => 'RAM', 'query_var' => true, 'rewrite' => true ) );
register_taxonomy( 'hard_drive', 'post', array( 'hierarchical' => true, 'label' => 'Hard Drive', 'query_var' => true, 'rewrite' => true ) );
}

Creating a Post Using your New Taxonomy

Creating post with classifications

Create a few new posts, and you’ll see your new taxonomy options appear in the Edit Post screen. Select whatever classifications you feel apply to your posts.


Showing a Post’s Various Taxonomies

Nothing we’ve done so far can be seen by your site visitors. We’d like for posts to show what custom taxonomies they’re classified in, just like posts commonly reveal their categories and tags.

To do so, we only need to make a simple addition to the loop in certain template files.


Displaying Taxonomy Classifications on Individual Pages

In the twentyten theme, and many others, a post’s categories and tags are listed below the body text. We’re going to add custom taxonomy information, if it exists, just before the category and tag information.

Taxonomy info on single posts

To make this happen, we’ll need to edit the “single.php” template file, which is normally called to display an individual post. My single.php file is at: [website_root]/wp/wp-content/themes/twentyten/single.php.


Step 1 Find the Right Place to Add Code

In single.php, find the line with:

<div class="entry-utility">

This appears just before the:

<div id="nav-below">

In twentyten, this div contains the categories, tags, permalink, and other data for the current post. We’ll put our taxonomy information just above this div.


Step 2 Retrieve Taxonomy Information About the Current Post

Populate some variables for holding the taxonomy information output and the different taxonomy information we may expect to find.

<?php
// Let's find out if we have taxonomy information to display
// Something to build our output in
$taxo_text = "";

// Variables to store each of our possible taxonomy lists
// This one checks for an Operating System classification
$os_list = get_the_term_list( $post->ID, 'operating_system', '<strong>Operating System(s):</strong> ', ', ', '' );

Here, we’re calling the WordPress function “get_the_term” list with the following parameters:

  • $post->ID : the id of the current post.
  • ‘operating_system’ : the name of the custom taxonomy we’re checking for data. We’re asking if the current post has been given any classifications in the ‘operating_system’ taxonomy.
  • ‘Operating System(s)’ : If anything is returned, this is the string we’d like to have in front of it.
  • ‘, ‘ : If multiple items are returned, this is the string we’d like to have them separated by.
  • : If anything is returned, this is the string we’d like to have behind it. In this case, we want nothing added behind the result.

We’ll do the same for the other two taxonomies we might expect to contain data:

$ram_list = get_the_term_list( $post->ID, 'ram', '<strong>RAM Option(s):</strong> ', ', ', '' );
$hd_list = get_the_term_list( $post->ID, 'hard_drive', '<strong>Hard Drive Option(s):</strong> ', ', ', '' );

More information:

Learn more about “get_the_term_list.”


Step 3 Format Results from Classifications, if Any

Check for results in each of the three possible taxonomies. If they exist, add them to our output, as well as a linebreak.

// Add OS list if this post was so tagged
if ( '' != $os_list ) {
    $taxo_text .= "$os_list<br />\n";
}
// Add RAM list if this post was so tagged
if ( '' != $ram_list ) {
    $taxo_text .= "$ram_list<br />\n";
}
// Add HD list if this post was so tagged
if ( '' != $hd_list ) {
    $taxo_text .= "$hd_list<br />\n";
}

Step 4 Display Classification Results, if Any

Check to see if the above steps resulted in any taxonomy information at all to output. If taxonomy info exists, we’ll output it wrapped in a div of class “entry-utility.”

// Output taxonomy information if there was any
// NOTE: We won't even open a div if there's nothing to put inside it.
if ( '' != $taxo_text ) {
?>
<div class="entry-utility">
<?php
echo $taxo_text;
?>
</div>
<?
} // endif
?>

Step 5 Check Your Results

Visit a post page, and you should see any custom taxonomy classifications listed below.

Taxonomy info on single posts 2

Viewing a List of Posts by Taxonomy Classification

Now our individual posts tell us what custom taxonomies they have been classified with. When they list a custom taxonomy classification, they also provide a link to list all posts under that classification. For instance, clicking the “Mac OS” link next to “Operating Systems” under our post will theoretically list all the posts with the “Mac OS” operating system classification.

However, this doesn’t happen out of the box with WordPress version 3. We’ll have to make a custom template file for displaying taxonomy archives in order for it to work. WordPress already lets visitors view all posts assigned to a particular category, or all posts given a certain tag. When we’re done here, we’ll be able to view all posts assigned to particular classifications in our custom taxonomies, too.

To make this happen, we’ll need to create the “taxonomy.php” template file. WordPress will try to use this file any time it wants to list posts in a custom taxonomy.


Step 1

Open the “category.php” file, copy its contents, and paste it into a new file called “taxonomy.php.” Save taxonomy.php in the theme directory. For instance, my taxonomy.php file is at:
[website_root]/wp/wp-content/themes/twentyten/taxonomy.php.


Step 2 Get Information About Current Taxonomy Classification

In the taxonomy.php file, we need to get information about the taxonomy being listed. We’ll probably want the name and description (if any) for the selected classification.

Just under <?php get_header(); ?>, add the following line:

$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );

This gets all of the information about the taxonomy that called this page and returns it as an object into the variable $term. For example, the “Mac OS” classification returns an object as such:

stdClass Object
(
    [term_id] => 13
    [name] => Mac OS
    [slug] => mac-os
    [term_group] => 0
    [term_taxonomy_id] => 22
    [taxonomy] => operating_system
    [description] =>
    [parent] => 0
    [count] => 2
)

Step 3 Display Classification Name and Description

We want to change the page name to tell visitors what they’re looking at. Since we started with the category.php template, we can take the line that used to print the category name and change it a bit to give us our desired page name and, if applicable, description.

Change the following line from category.php:

printf( __( 'Category Archives: %s', 'twentyten' ), '<span>' . single_cat_title( '', false ) . '</span>' );

To read as follows:

printf( __( 'Posts classified under: %s', 'twentyten' ), '<span>' . $term_name . '</span>' );

This changes the static text beginning the line, and then inserts the name of the classification. (Note: for proper localization, we would need to add ‘Posts classified under:’ correctly to the languages/twentyten.pot file. That’s outside the scope of this tutorial, but be aware of the transgression here.)

Then add the following:

if ('' != $term_descr ) {
echo "<p>$term_descr</p>\n";
}

If a description exists for this classification, it will be displayed just beneath the title.

Taxonomy Archive Page

After making changes to taxonomy.php, visit one of your posts that has been given a custom taxonomy classification. Because of our earlier work in the file “single.php,” the post should show custom classifications below it. Simply click one of those classifications to see the taxonomy listing at work.


Conclusion

I hope this tutorial explained clearly what taxonomies are and showed you how to make use of them in WordPress 3 as a powerful organizational tool. I hope to provide a follow up tutorial soon explaining WordPress custom post types, their tight relationship with custom taxonomies, and how to use them. Thanks so much for taking the time to visit Nettuts!

Tags: Wordpress
Note: Want to add some source code? Type <pre><code> before it and </code></pre> after it. Find out more
  • weird

    how can i list entries linked to custom taxonomy ??!?!

    • http://www.winningsem.com WinningSEM

      where languages is the taxonomy

      and people is your post type

      ‘people’,
      ‘languages’ => ‘My Lang”,
      ‘order’ => ‘ASC’, // ‘ASC’ – ascending order from lowest to highest values (1, 2, 3; a, b, c). ‘DESC’ – descending order from highest to lowest values (3, 2, 1; c, b, a).
      ‘posts_per_page’ => ’2000′,
      ‘meta_key’ => ‘people_order’,
      ‘orderby’ => ‘meta_value_num’, //possible values id,url,name,target,description,owner,rating,rel,notes,rss,length,rand,

      );
      query_posts( $args );
      while ( have_posts() ) : the_post();
      ?>

  • http://www.lovelytemplates.com Prabu

    This post is awesome. Good job.

  • Will Castillo

    Hello,

    I’m been trying for days to achieve a particular behaviour in vain :(

    I’ve a site that is heavily based on Custom Post Types, Custom Taxonomies and Custom Fields.

    For instance, Movies.

    Say I have a Custom Post Types named Movies.
    Two Custom Taxonomies named Actors and Directors.
    Plus many Custom Fields regarding the movie.

    What I’m trying to do is that whenever a visitor search for something, the WordPress search inside the Custom Fields and Taxonomies of my Custom Post Types.

    Ie.- Someone search for First Contact… and WP should return the Custom Post for Star Trek First Contact… and if someone look for Jonathan Frakes, then WP should also include Star Trek First Contact in the results (given that Jonathan Frakes was his Director).

    I don’t have any traditional post or page… And my Custom Post Types does not even have Content nor Excerpt. Everything is handled using my own Custom Fields.

    Does anyone knows a way yo make it works?

    Thanks in advance,
    Will

    • http://studiotwo.com Lance Monotone

      @Will Castillo,

      I use a plugin called Search Everything to search my custom taxonomies and custom fields. It works pretty well and will also highlight the results.

  • Bira

    Hi,

    I’m wondering if I could perhaps get the answer here.

    To make my question, I’ll use an example of movies.

    I have a new post_type called movies, and to that post_type I added the following custom taxonomies:

    - Directors (non-heirarchical, so like tags)
    - Actors (ditto)
    - Genres (action? romcom? etc. selected ‘heirarchical’ so like categories).

    The thing is, for Actors and Directors, I want to add information beyond the option of slug and description. I want to add a photo, for example. But there’s no option that I can see in WordPress to do that.

    Any suggestions how I can do this with using custom taxonomy? I know how to do that independently, so to speak – i.e., do a “plugin” for that task that utilises a new db table, some custom fields, etc. But I’m wondering if there’s no easier way of doing it?

    I get the feeling the custom taxonomy in WordPress it’s somewhat under-developed, as it stands. If I am wrong, I would dearly love to be educated.

    Thank you :)

    • Becky

      Bira, did you ever find a resolution to this? I’m in a similar situation. I’m setting up a custom taxonomy to use as a Glossary of terms; authors will choose terms relevant to their article, and those definitions will appear in a sidebar. I can get away with just using the description, but it would be great extra fields could be added (for example, a URL field for additional information).

      Would be interested to know how you resolved your problem. :)

    • http://www.twitter.com/nomar Ramon

      Hi Bira and Becky.

      I was wondering too and found this Blogpost
      http://justintadlock.com/archives/2010/08/20/linking-terms-to-a-specific-post

      He defines both, a taxonomy to structure things and a custom post type to hold additional data.

      HTH,
      Ramon

  • http://maparent.ca/ Marc-Antoine Parent

    I thought I’d let people know about a design flaw. I tried to create a taxonomy that would cover links, attachments and posts. At the database level, the wp_term_relationship table uses a object_id column that may refer either to wp_post.ID or wp_links.link_id, depending on what the taxonomy is registered to cover. If your taxonomy refers to both links and non-links (most wp types fall in the wp_posts table), you may not know whether your relationship refers to either, because of ID collisions between those tables. Annoying.

  • http://www.a1media.ca Douglas Helmer

    Great tutorial :)

    Just a note to others who may be encountering a problem I had: in Step 4, “Display Classification Results, if Any” on line 10, the opening PHP tag is only a short tag. This caused a page load error on my local WAMP install.

    // Output taxonomy information if there was any
    // NOTE: We won’t even open a div if there’s nothing to put inside it.
    if ( ” != $taxo_text ) {
    ?>
    <div class=”entry-utility”>
    <?php
    echo $taxo_text;
    ?>
    </div>
    <? // The problematic short tag
    } // endif
    ?>

    I changed it to a full tag, as shown below, and everything worked properly again:

    // Output taxonomy information if there was any
    // NOTE: We won’t even open a div if there’s nothing to put inside it.
    if ( ” != $taxo_text ) {
    ?>
    <div class=”entry-utility”>
    <?php
    echo $taxo_text;
    ?>
    </div>
    <?php //Changed to full tag!
    } // endif
    ?>

    Thanks again for such a useful tutorial.

    • Olga

      Thank you so much! I would’ve never found this problematic tag.

  • ezhil

    hello
    iam using custom taxonomies in my site,so i did have seperate templates for displaying them.
    i have created a taxonomy called people and a term called star and also respective template to star (taxonomy-people-star.php) every thing works fine http://sportsstatus.in/dev/people/star/ .
    but when i query some other taxonomy like http://sportsstatus.in/dev/people/star/?sports=tennis to it the template structure changes to the archive format. how to over this.
    i want the original template structure assigned for star(taxonomy-people-star.php) to work.

    • Rich Gilberto

      Then why did you make it taxonomy-people-star.php? If you just make it taxonomy-people.php it will be the template for the whole “people” taxonomy.

      Word up.
      Rich

  • Adam

    Hi. These is an error in the code in one of the tutorial steps. Under Display Classification Name and Description the code reads:

    printf( __( ‘Posts classified under: %s’, ‘twentyten’ ), ” . $term_name . ” );

    this should be:

    printf( __( ‘Posts classified under: %s’, ‘twentyten’ ), ” . $term->name . ” );

    • http://laurafries.com laura

      Great catch, thanks!

      • http://other Oli

        I’ve been wondering for hours why this wasn’t working! Thank you!

      • juanlink

        Thanks for the correction!

    • http://urbanlegendweb.co.nz Duncan O

      The same goes for $term_description. Should be $term->description. cheers

  • http://themeplay.com/ hafeesh

    Great tutorials

  • http://simonong.net Simon

    Nice article!

    Is there a way to retrieve the information outside the loop?

  • Kaiser

    Ad rewrite) You don’t have to set it to “true”. In the register_taxonomy() function, you can specify your own slug like this:

    ‘rewrite’ => array( ‘slug’ => ‘your_custom_slug_name’ )

  • http://iphoneg.com hrose

    nice tutorials

  • http://www.iphone-new.com rajamd

    Nice article! Thank you so much

  • Rich

    The part at the end about changing around category.php is pretty specific to the Twenty Ten theme. What if we’re using a theme that doesn’t have category.php or archive.php? Should we use index.php?

  • henkeman

    Great post!

    I have a question: I want to assign a custom taxonomy to wp’s own link object type, like this:

    register_taxonomy(‘interests’, array(‘post’,'link’), array(
    ‘hierarchical’ => true,
    ‘labels’ => $labels,
    ‘show_ui’ => true,
    ‘query_var’ => true,
    ‘rewrite’ => array( ‘slug’ => ‘interests’ )
    ));

    I’ve tried it but never got it working.

    From what I gather from the above article3 it should be possible. Is this a bug? Or is it just not possible? Has anybody else succeded with this?

    Thanks,
    /henrik

  • http://www.jystdesign.co.uk Jyst

    Great insight into Taxonomies – thanks for the in-depth article!

    Tip:

    To extend ‘Viewing a List of Posts by Taxonomy Classification’ and view the actual taxonomies in the results as well as the categories just add the same code from Step 2 Retrieve Taxonomy Information About the Current Post, and put it inside the loop next where list categories is handled (line 145 in loop.php for TwentyTen)

    Your category listings will now also display your taxonomies. =)

    • http://www.jystdesign.co.uk Jyst

      Correction: line 150 in loop.php – after ‘get_the_category_list php’ statement and before the ‘get_the_tag_list’

  • WillR

    What would need to be changed on this line if using a different theme?

    printf( __( ‘Posts classified under: %s’, ‘twentyten’ ), ” . $term_name . ” );

    ‘twentyten’ changed to ‘yourthemename’?

  • http://Toledo paupboAlo

    You’ll never get addicted to a painkiller if you consult with your doctor and follow instructions. That’s what i want to say here.

  • aamir ali khuwaja

    nice explaination……..

  • Aamir

    this is nice explaination i have understand each and every thing..now can any one tell me how to create custom tags in wordpress 3.0.5..using PHP?
    for example…post, page,link,etc
    our created tag work same like they work..plz code with litle bit explaination with comments and explain every thing and then send it to aamirjaan28@gmail.com thx

  • http://www.phpvibe.com Dorel Cristea

    I’ve registred my taxonomies like this:

    add_action( ‘init’, ‘build_taxonomies’, 0 );

    function build_taxonomies() {
    register_taxonomy( ‘varumarken’, ‘post’, array( ‘hierarchical’ => true, ‘label’ => ‘Varumärken’, ‘query_var’ => true, ‘rewrite’ => array( ‘slug’ => ‘varumarken’ ) ) );
    register_taxonomy( ‘herr_varumarken’, ‘post’, array( ‘hierarchical’ => true, ‘label’ => ‘Herr Varumärken’, ‘query_var’ => true, ‘rewrite’ => array( ‘slug’ => ‘herr-varumarken’ ) ) );
    register_taxonomy( ‘dam_varumarken’, ‘post’, array( ‘hierarchical’ => true, ‘label’ => ‘Dam Varumärken’, ‘query_var’ => true, ‘rewrite’ => array( ‘slug’ => ‘dam-varumarken’ ) ) );
    }

    Everything works fine, but when I turn on the rewrites…all permalinks return 404 on taxonomies :(

    What did I did wrong? It’s a 3.1 wp

  • http://www.phpvibe.com Dorel Cristea

    Oh…this fixed it:

    add_action( ‘init’, ‘build_taxonomies’, 0 );

    function build_taxonomies() {
    register_taxonomy( ‘varumarken’, ‘post’, array( ‘hierarchical’ => true, ‘label’ => ‘Varumärken’, ‘query_var’ => true, ‘rewrite’ => array( ‘slug’ => ‘varumarken’, ‘with_front’ => false ) ) );
    register_taxonomy( ‘herr_varumarken’, ‘post’, array( ‘hierarchical’ => true, ‘label’ => ‘Herr Varumärken’, ‘query_var’ => true, ‘rewrite’ => array( ‘slug’ => ‘herr-varumarken’, ‘with_front’ => false ) ) );
    register_taxonomy( ‘dam_varumarken’, ‘post’, array( ‘hierarchical’ => true, ‘label’ => ‘Dam Varumärken’, ‘query_var’ => true, ‘rewrite’ => array( ‘slug’ => ‘dam-varumarken’, ‘with_front’ => false ) ) );
    }

    I’ve found a reference and added ‘with_front’ => false

  • Dave

    I still don’t understand the difference between taxonomies and categories. The example given of operating systems can still be achieved with just plain old built in categories. Simply make sub categories.

    under the category operating system, why not make a sub category for each kind you want…. Mac, windows, linux. And then under each of those, make sub categories for the specific os.

    Why go through this hassle?

  • http://www.mattpealing.co.uk Matt

    Great tutorial, exactly what I needed :D

  • http://www.re-cycledair.co Jack

    Excellent post! I can’t believe I’ve never taken the time to learn taxonomies! The possibilities with these for WordPress as a CMS are endless!

  • EkDor

    No matter what I do I can’t get the taxonomy.php to load. All I ever get is the 404 page. The taxonomy data loads in the index.php, search.php and singles.php. I have tried it a couple of times, read through the comments tried everything suggested. :(

    • Ekdor

      Additionally I tried it on the Sand Box theme as well as another site based on it..

    • EkDor

      Could I trouble someone to modify a copy of the Sand Box theme, which available in WP Theme search under that name. Create a couple of custom taxonomies and I’ll use that to get mine working. If I can’t then I’ll at least know it’s something else causing it.

      Post a link in a comment for me others to download. I’ll keep an eye on article/tutorial and reply when I have a copy.

      Thanks in advance to someone who does this for me. Greatly appreciated. Cheers,.

      P.S. Despite my trouble it’s a great tutorial. I just can’t figure where I’m going wrong.

    • EkDor

      Hmm No idea why but it started to work. As far as I can tell I didn’t change any of the code. Only data in the wordpress back end. No it’s not to do with refreshing the browser.

      But now for some reason all but one of my taxonomies works. For some reason a numeric taxonomy fails. Its called slug is year and the Year taxonomy items all contain numeric Names and slugs. Tried changing out the numeric slugs but that only results in loading the main feed.

      Any ideas

    • EkDor

      oh I forgot to clarify. The numeric issue returns a blank taxonomy page. All the data is there except there are no posts listed. I tried clearing all the items and re-entering them. Have looked closely at the year code compared to its siblings and all is correct as far as I can see.

    • EkDor

      Sorry about the long list of posts. Just noticed that some of my years will return results and some will not. It’s a bit weird. Thinking it not the implementation of the custom taxonomies but rather some kind of WP bug related to numeric slugs. Perhaps the 4 digit numeric slug, my post short slugs are also 4 digit numeric. But this doesn’t explain why my alpha slugs just reload the main feed. I will try that again before posting this comment in case my new items are different in some way. … Ok replaced all the slugs within the Year custom taxonomy to alpha; for example “two-thousand-eleven” and again they just load the main feed. Perhaps it’s related to the Name of the item and not just the slug. Which offers an interesting dilemma … Hmm tried all sorts of permutations. Now the ones that worked no longer work. Tried it on other browsers that have never accessed the domain and I get the exactly the same results.

    • EkDor

      Hmm Going to stop posting after this and wait for some kind of response. But before I post this. I changed the ID in the function thinking the ID year might be part of it, but no change. Although after another attempt to update the init name the year taxonomy now returns a steady 404 again.

    • EkDor

      Oh I know I said I wasn’t going to post, but something of particular interest came to my attention. The Custom Taxonomy has appeared as a new category list in the menu too of WP back end. That is bellow the categories list of items to add to a menu is now a Year list. None of the other taxonomies have showed up there. Beats me why this is. But upon adding some of the year items to the menu I still get 404.

    • EkDor

      WOOOOHOOOO!!!!!!

      Ok found the problem. In my perma-links settings on the WP Back end I had a custom structure with:

      /%post_id%

      This was causing some conflict with the year numerics. Or so I can assume.

  • http://www.clivetsoft.cl Alex

    Thanks alot!

  • http://www.fernandohrosa.com.br/en/ Fernando H Rosa

    Great article! Thank you for putting this up. Very well explained and easy to follow. Before seeing your article I wasn’t too sure how to use custom taxonomies (or even I should use them at all on my blog) but you made it crystal clear.

  • http://www.inforumah.com/ Nasrudin

    Taxonomy problem has solved. This tuts helped me more for my site. Thanks much.

  • Rafael

    Nice Tutorial, congratulations!

  • emma

    hi – excuse me if this is a beginner question, but in step “Display Classification Name and Description” , where you use this line:

    printf( __( ‘Posts classified under: %s’, ‘twentyten’ ), ‘<span>’ . $term_name . ”<span>’ );

    what would i replace ‘twentyten’ with if i was using a different theme, and where would i find out what to replace it with, in the case that its a user-defined theme name set by whoever created the theme?

    your tut is great its just for a beginner like me, where all the other bits of code were broken down a bit more and this step wasnt, i found it difficult to ‘transfer’ the step into context of my different theme which doesn’t actually have a line beginning printf(__ in category.php or my new taxonomy.php file i’ve created from category.php.

    thanks very much :)
    emma

  • http://www.banglagirlsclub.com anika

    Great Tutorial without any doubt. But I just want to show list of post in a new page according to a category. Can you please tell how to do this?

  • http://nicolas.courtois.com.fr/ Nicolas

    Many thanks for that great tutorial.

    Is there any way to populate the taxonomies classification others than the dashboard ?

    Nicolas

  • http://www.laura-kinoshita.com Laura Kinoshita

    I deleted about eight custom taxonomies at once using a pre-programmed taxonomies.php file I bought from a PlugIn programmer, and my site seems to have gotten destroyed.

    I get a white screen when I try to access any page on the domain.

    No error message, just a completely white screen.

    Apparently when I deleted the custom taxonomy codes, the .php program I was using must have deleted a system file in the WordPress installation?

    Any idea which file this could possibly be?

    My hosting company is willing to help, if I can only suggest which file may be corrupted. It’s a problem they’ve never come across.

    Thoughts of how to repair, or what could be causing this blank screen?

  • Dan

    The easiest way for terms order is this plugin Advanced Taxonomy Terms Order

  • Anderson

    Categories have the file category.php for list all post by “Categories”, and Taxonomies? :S taxonomy.php? Because dont work… I need list the post by taxonomies, but site.com/custom_post_type/custom-post and not site.com/taxonomy/custon-post-type/custom-post..

  • Richard

    Doesn’t seem to be working for me. Using WP 3.2.1, hosted remotely.
    When I cut and paste the function code in this tutorial, what I get is the text of the code displayed across the top of my screen, followed by the WP dashboard; but no new taxonomy option listing in the Posts section.
    What am I doing wrong?

    • http://www.creaturez.org Corey

      I’m having the exact same problem. Is this a theme issue? Has anyone found a fix for this?

      • Happeh

        Look and see if the code that is printed on your website is inside of the loop.

        If it is, move it up above the loop and see what happens.

    • Jonah Powell

      This is also happening to me. I feel like I followed the code instructions carefully, but I’m getting the same result… no created taxonomies and the code appearing behind the dashboard header. Any help would be appreciated.

      • http://www.iwantdavid.com David

        To those of you who are seeing source code appear on your site: Where are you putting the code?

        I haven’t tried this example but when I add custom taxonomies – or add any functionality to WordPress for that matter – I tend to create a new plugin or theme, it’s not very difficult at all and is the only accepted practice for completing

        Try checking out the WordPress docs for creating a new plugin, and also consider using the mu-plugins feature of WordPress (but if you do be warned there are some subtle differences between a ‘must use’ plugin and the standard kind, while this does easily avoid accidental disable situations.)

        http://codex.wordpress.org/Writing_a_Plugin
        http://codex.wordpress.org/Must_Use_Plugins

  • http://miramichionline.com Larry

    I have a site that has multiple contributors who contribute to 2 categories. Example, User1 posts to “User1_Events” and “User1_News”. Using the information above I am trying to figure out if there is a way to create a page template for User1 that will display lists of posts in each category??

  • http://getcheapcarinsurance.x10.mx/ Harsh maur

    hey i really struggled to add custom taxonomy to my site. with this useful post i was easily able to add taxonomy to my site. many thanks for it!

  • http://www.markhoult.com Mark

    Still finding taxonomies really, really tricky to implement.

    Ideally they should work for slicing and dicing your posts in a two different dimensions (eg when used alongside your categories), but boy! is it tricky to get it to work…….

  • Pavel

    How come’s everybody is saing thanks for tutorial wich doesn’t work??

    Ok I get it it’s old. This taxonomy thing is just crazyyyy.

  • http://www.indezoo.com/ Idris

    a great tutorial…thank you for sharing!!!

  • http://gtwebworx.com/ gtwebworx

    You know why I love tutsplus? it’s because of their excellent , we’ll laid out articles/tutorials. :-)

  • http://how2blog.in …:::| ARAfiN SHa0N |:::…

    Works pretty fine :) Thank u so much sir that’s what i’ve been looking for so long :) Finally i am able to do this in my blog.

  • mark shirley

    Thanks a great tutorial but does anyone know how to apply this to twentyeleven as there is no loop file to alter and I would like to know how to get taxonomies to display on the post or page in twentyeleven…Thanks

  • http://www.socialmediaplan.gr social media plan

    This is the best post I’ve seen on wordpress taxonomies. Really good job!

  • eirik

    Great tutorial indeed, but for me as new to web development. I want a tutorial like this that also covers how to output that custom taxonomy into taxonomy page or single taxonomy page. I’m sorry about my english.

  • Jose

    I tried this tutorial. I am able to create three taxonomies in the write posts section in the admin panel. But unable to display the taxonomy information for a post & I can’t view the list of posts under each taxonomy.

    I created an “Operating System” – Windows and added a post. But mysite.com/operating_system/windows/ gives a 404 page. Same is the case for all there taxonomies.

  • Patrick

    Great Tutorial and I got it to work in under 20 minutes.

    Still I need some help. How is it possible to just show the first child of a parent tag?

    regards

    Patrick

  • http://www.elan42.com/ Alvise Nicoletti

    After reading this GREAT article I was searching a way to make a Menu LISTING the custom taxonomies I created.

    I resolved with this: http://wordpress.org/support/topic/how-to-get-list-custom-taxonomy-terms-for-specific-taxonomy

    More specifically, with this code:
    //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
    );