3

I have a custom post type called 'designs' and have registered taxonomy 'design_category' and 'design_tag'.

I'm trying to get the permalinks as follows but not having much luck - seems having 2 taxonommies is causing only one to work - either 'design_category' or 'design_tag'

I want the permalinks like this:

a single design: url.com/designs/design_category/single-design

a design category: url.com/designs/design_category

a design tag: url.com/designs/design_tag

there are only a few specific categories and not much room for expansion there so there won't ever be a matching category and tag.

this is the code I have to create everything - just stuck on getting the tags to work - keeps going to 404 error :(

function create_design() {
  // Design Tags
  $labels = array(
'name' => _x( 'Design Tags', 'taxonomy general name' ),
'singular_name' => _x( 'Design Tag', 'taxonomy singular name' ),
'search_items' =>  __( 'Search Design Tags' ),
'all_items' => __( 'All Design Tags' ),
'parent_item' => __( 'Parent Design Tags' ),
'parent_item_colon' => __( 'Parent Design Tags:' ),
'edit_item' => __( 'Edit Design Tag' ), 
'update_item' => __( 'Update Design Tag' ),
'add_new_item' => __( 'Add New Design Tag' ),
'new_item_name' => __( 'New Design Tag Name' ),
'menu_name' => __( 'Design Tags' ),
  );    

  register_taxonomy('design_tag',array('design'), array(
'hierarchical' => false,
'labels' => $labels,
'show_ui' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'designs' ),
  ));

  // Design Categories
  $labels = array(
'name' => _x( 'Design Categories', 'taxonomy general name' ),
'singular_name' => _x( 'Design Category', 'taxonomy singular name' ),
'search_items' =>  __( 'Search Design Categories' ),
'all_items' => __( 'All Design Categories' ),
'parent_item' => __( 'Parent Design Category' ),
'parent_item_colon' => __( 'Parent Design Category:' ),
'edit_item' => __( 'Edit Design Category' ), 
'update_item' => __( 'Update Design Category' ),
'add_new_item' => __( 'Add New Design Category' ),
'new_item_name' => __( 'New Design Category Name' ),
'menu_name' => __( 'Design Categories' ),
  );    

  register_taxonomy('design_category',array('design'), array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'designs' ),
  ));  

// Create post type
$labels = array( 
    'name' => _x( 'Designs', 'design' ),
    'singular_name' => _x( 'Design', 'design' ),
    'add_new' => _x( 'Add New', 'design' ),
    'add_new_item' => _x( 'Add New Design', 'design' ),
    'edit_item' => _x( 'Edit Design', 'design' ),
    'new_item' => _x( 'New Design', 'design' ),
    'view_item' => _x( 'View Design', 'design' ),
    'search_items' => _x( 'Search Designs', 'design' ),
    'all_items' => __( 'All Designs', 'design' ),
    'not_found' => _x( 'No Designs found', 'design' ),
    'not_found_in_trash' => _x( 'No Designs found in Trash', 'design' ),
    'parent_item_colon' => _x( 'Parent Design:', 'design' ),
    'menu_name' => _x( 'Designs', 'design' ),
);

$args = array( 
    'labels' => $labels,
    'hierarchical' => false,

    'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'trackbacks', 'revisions' ),
    'taxonomies' => array( 'design_categories', 'design_tags' ),
    'public' => true,
    'show_ui' => true,
    'show_in_menu' => true,
    'menu_position' => 5,

    'show_in_nav_menus' => true,
    'publicly_queryable' => true,
    'exclude_from_search' => false,
    'has_archive' => 'designs',
    'query_var' => true,
    'can_export' => true,
    'rewrite' => array( 
        'slug' => 'designs/%design_category%' ),
    'capability_type' => 'post'
);

register_post_type( 'design', $args );

}

add_filter('post_type_link', 'design_term_permalink', 1, 3);
function design_term_permalink($post_link, $id = 0, $leavename = FALSE )
{
   if ( strpos('%design_category%', $post_link) === 'FALSE' ) {
  return $post_link;
}
$post = get_post($id);
if ( !is_object($post) || $post->post_type != 'design' ) {
  return $post_link;
}
$terms = wp_get_object_terms($post->ID, 'design_category');
if ( !$terms ) {
  return str_replace('designs/%design_category%/', '', $post_link);
}
return str_replace('%design_category%', $terms[0]->slug, $post_link);}

1 Answer 1

3

I answered a similar question but with the details of how to do it yourself:

Adding categories to custom post type in permalink

2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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