Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

James Home
James Home
12,011 Points

Custom taxonomy in URL as category

It is my first time to create a custom taxonomy. I am trying to create a taxonomy for articles categories but I am not doing very well. Ideally I'd like for the end result to look like:

domain.com/custom_taxonomy/post_type
domain.com/article_category/article

However it ends up in either of these two:

domain.com/%article_category%/dogs
domain.com/article/dogs
function taxonomy_article() {
   $labels = array(
    'name'              => _x( 'Article Categories', 'taxonomy general name' ),
    'singular_name'     => _x( 'Article Category', 'taxonomy singular name' ),
    'search_items'      => __( 'Search Article Categories' ),
    'all_items'         => __( 'All Article Categories' ),
    'parent_item'       => __( 'Parent Article Category' ),
    'parent_item_colon' => __( 'Parent Article Category:' ),
    'edit_item'         => __( 'Edit Article Category' ), 
    'update_item'       => __( 'Update Article Category' ),
    'add_new_item'      => __( 'Add New Article Category' ),
    'new_item_name'     => __( 'New Article Category' ),
    'menu_name'         => __( 'Article Categories' ),
  );
  $args = array(
    'labels' => $labels,
    'hierarchical' => true,
    'rewirte' => array('slug' => 'articles/%article_category%', 'with_front' => true),
  );
  register_taxonomy( 'article_category', 'article', $args );
}

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


function article_init() {
    $args = array(
      'label' => 'Article',
        'description' => 'Article to write',
        'public' => true,
        'show_ui' => true,
        'capability_type' => 'post',
        'hierarchical' => true,
        'menu_position' => 5,
        'has_archive' => true,
        'rewrite' => array('slug' => 'articles/%article_category%', 'with_front' => true),
        'with_front' => true,
        'menu_icon' => 'dashicons-format-quote',
        'supports' => array(
            // 'title',
            // 'editor',
            // 'custom-fields'
            )
        );
    register_post_type( 'article', $args );
}

add_action( 'init', 'article_init' );

1 Answer

Tim Knight
Tim Knight
28,888 Points

James, normally I use Custom Post Types UI to handle this just to decouple the functionality from the theme (in case the user changes the theme it doesn't remove the URL functionality). That being said it looks like you have the word rewrite written as rewirte in the taxonomy_article function.

James Home
James Home
12,011 Points

Thanks Tim.

I like to do things the hard way so I can learn as much about WordPress as possible. I still appreciate your answer and I have corrected the typo. I ended up appending this code to the end of my plugin.

Tim Knight
Tim Knight
28,888 Points

Oh I definitely know what you mean, it's something good to know in terms of how to do it manually. I was just speaking to the importance of decoupling from the theme in many cases. I'm glad it sounds like things are working for you now.