Categories and tags are great, you can really do a lot with them when it comes to filtering queries see here. But a ton of categories can really look messy in the backend and confuse your editors (or you at 4am). Here is a quick run down of WordPress’ register_taxonomy function. The code below can go anywhere in your functions.php file or you can create a plugin and upload it, either way it is simple and to the point. It looks a lot like creating a Custom Post Type to be honest.


add_action( 'init', 'register_comic_taxonomy' );

function register_comic_taxonomy() {
    register_taxonomy( 'comic', 'post', array(
        'labels' => array(
            'name' => _x( 'Comic Publishers', 'taxonomy general name'),
            'singular_name' => _x( 'Comic Publisher', 'taxonomy singular name' ),
            'search_items' => __( 'Search Comic Publishers' ),
            'all_items' => __( 'All Comic Publishers' ),
            'parent_item' => __( 'Parent Comic Publisher' ),
            'parent_item_colon' => __( 'Parent Comic Publisher:' ),
            'edit_item' => __( 'Edit Comic Publisher' ),
            'update_item' => __( 'Update Comic Publisher' ),
            'add_new_item' => __( 'Add New Comic Publisher' ),
            'new_item_name' => __( 'New Comic Publisher Name' ),
            'menu_name' => __( 'Comic Publisher' ),
        ),
        'hierarchical' => true, // Makes for a better selection box on write screen
    ) );
}

There are a few more options on the WordPress Codex site you can have a look at but I find the ones listed are all I needed to set up a ‘Comic Publisher’ taxonomy. This frees up a category parent/children from the ‘Categories’ box on the post editing page and neatly creates a new box that I can add a publisher just as easily as a category. Tomorrow’s post I’ll show you how to add the custom taxonomy to your permalink structure.

Did you wait?

If so here is the code to add the custom taxonomy to your permalink structure. I’ve been doing some research and there are a few different ways to do it but basically they all work the same. Here is what I use;


add_filter('post_link', 'animal_permalink', 10, 3);

function animal_permalink($permalink, $post_id, $leavename) {

    if (strpos($permalink, '%comic%') === FALSE) return $permalink;

        // Get post
        $post = get_post($post_id);
        if (!$post) return $permalink;
        // Get taxonomy terms
        $terms = wp_get_object_terms($post->ID, 'comic');
        if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0])){
                $taxonomy_slug = $terms[0]->slug;
         } else {
                $taxonomy_slug = '';
                $permalink = str_replace('%comic%/', '', $permalink);
        }

    return str_replace('%comic%', $taxonomy_slug, $permalink);

}

One major thing to note, most of the code I’ve found online had a much different ‘else’ statement at the end, which basically is determining what the $taxonomy_slug will be if the post does not have a ‘Comic’ taxonomy applied. Most of the time you’ll see the solution look like this:


} else {
                $taxonomy_slug = 'comic';
}

This makes perfect sense, it sets a ‘comic’ as the default replacement for %comic%. My solution takes it out completely. Either way should work fine, I have yet to come across any issues (though I don’t actively use this permalink structure on this site, I do on others).

Once you’ve dropped the code into your functions.php (or turned into a plugin) just add %comics% to your permalink structure and watch the magic happen.

  • http://www.bythegram.ca Adam Graham

    I’d love to hear how you use this tutorial, any problems or success stories!

  • AdamGraham

    Hi all! I am testing out the Disqus commenting system for the site, should I keep it?