0

I've searched around and have not found the answers im looking for. By default I have:

  • example.com/blog (blog archive)
  • example.com/blog/entry (single entry)
  • example.com/blog/category/announcements (single taxonomy view)

Now I've created a custom post type & taxonomy (categories) to go along with it, but I'm having trouble mirroring the blog's URL structure. For example my goal is:

  • example.com/talks (talks archive)
  • example.com/talks/entry (single talk entry)
  • example.com/talks/category/support (single taxonomy view)

Here is what I have so far:

function uc_add_talks_post_type() {
  $talks_labels = array(
    'name'                => 'Talks',
    'singular_name'       => 'Talk',
    'all_items'           => 'All Talks',
     …
  );

  $talks_args = array(
    'labels'             => $talks_labels,
    'public'             => true,
    'publicly_queryable' => true,
    'show_ui'            => true,
    'menu_position'      => 5,
    'rewrite'            => array( 'slug' => 'talks', 'with_front' => false ),
    'has_archive'        => 'talks',
  );

  $talks_cat_args = array(
    'hierarchical' => true,
    'labels' => array(
      'name' => 'Talk categories',
      'singular_name' => 'Talk category',
      …
    ),
    'show_admin_column' => true,
    'show_ui' => true,
    'query_var' => true,
    'rewrite' => array(
      'slug' => 'talks',
      'with_front' => false
    ),
  );

  register_post_type( 'uc_talks', $talks_args );
  register_taxonomy( 'uc_talks_cat', array('uc_talks'), $talks_cat_args );
}

add_action( 'init', 'uc_add_talks_post_type');
  • What is your specific trouble? What is not happening/working with this code that you expect to work? – Rarst Dec 28 '13 at 20:27
  • I can't figure out how to get the talk categories URL to be example.com/talks/category/support. It's currently example.com/talks/support and that is returning a 404 (i do have a taxonomy-uc_talks_cat.php) page. It's not really that I expect that URL to be working, I know im missing something, but i've tried a bunch of things and can't figure out what I should be doing. – blg002 Dec 28 '13 at 23:15
0

I figured this out by reading this article. It was very simple, all I had to do was:

  1. Change the $talks_cat_args[rewrite][slug] from 'talks' to 'talks/category' (which I had tried before) but missed…
  2. Make sure register_taxonomy is before register_post_type.

Read the article, but it's basically because of "how WP_Rewrite works as the process of deciphering permalinks is, in a way, top-down"

For posterities sake here is the final code (with a little reformatting). I can't argue that this is the best or cleanest way to do this, cause I'm pretty new to WordPress & PHP, but it's working thus-far.

 $talks_labels = array(
    'name'                => 'Talks',
    'singular_name'       => 'Talk',
    'all_items'           => 'All Talks',
    'add_new'             => 'Add new',
    'add_new_item'        => 'Add new Talk',
    'edit_item'           => 'Edit Talk',
    'new_item'            => 'New Talk',
    'view_item'           => 'View Talk',
    'search_items'        => 'Search Talks',
    'not_found'           => 'No Talks found',
    'not_found_in_trash'  => 'No Talks found in Trash',
  );

  $talks_args = array(
    'labels'             => $talks_labels,
    'public'             => true,
    'publicly_queryable' => true,
    'show_ui'            => true,
    'menu_position'      => 5,
    'rewrite'            => array( 'slug' => 'talks', 'with_front' => false ),
    'has_archive'        => 'talks',
  );


  $talks_cat_labels= array(
    'name'              => 'Talk categories',
    'singular_name'     => 'Talk category',
    'search_items'      => 'Search talk categories',
    'all_items'         => 'All talk categories',
    'parent_item'       => 'Parent talk category',
    'parent_item_colon' => 'Parent talk category:',
    'edit_item'         => 'Edit talk category',
    'update_item'       => 'Update talk category',
    'add_new_item'      => 'Add new talk category',
    'new_item_name'     => 'New talk category name',
  );

  $talks_cat_args = array(
    'hierarchical' => true,            
    'labels' => $talks_cat_labels,
    'show_admin_column' => true, 
    'show_ui' => true,
    'query_var' => true,
    'rewrite' => array(
      'slug' => 'talks/category',
      'with_front' => false
    ),
  );

  register_taxonomy( 'uc_talks_cat', array('uc_talks'), $talks_cat_args );
  register_post_type( 'uc_talks', $talks_args );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.