custom post type permalinks with category and tags

custom post type permalinks with category and tags

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);}

Solutions/Answers:

Answer 1:

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

Adding categories to custom post type in permalink

References

Permalink Issues by Installing WordPress in Subdirectory / Subfolder

Permalink Issues by Installing WordPress in Subdirectory / Subfolder

I installed WP in subfolder /cms and it runs live without subfolder. When I use /%postname%/ as permalink I get 404 error on my site. My Server supported mod_rewrite.
My Code
wp-config.php
define('WP_SITEURL', 'http://www.sitename.de/cms');
define('WP_HOME', 'http://www.sitename.de');

root
1) .htaccess

RewriteEngine On
RewriteBase /cms/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /cms/index.php [L]


2) index.php
  

/cms
1) .htaccess

RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]


2) index.php
  

Thanks for your help
Ogni

Solutions/Answers:

Answer 1:

What I generally do

  • Install WP in sub directory.
  • Open permalink set required permalink.
  • Open settings->general link
  • Remove the sub directory name from ‘Site Address (URL)’
  • Cut the index.php and .htaccess files from sub directory and paste them to root
  • change code in index.php as you did.
  • Again access settings->permalink save the form

Done. You dont need index.php and .htaccess file in sub directory.

Answer 2:

Get rid of the .htaccess file under /cms and then visit Settings -> Permalinks to flush your rewrite rules. Also make sure the site URL and home URL are set up in the database correctly (like you have in the wp-config).

Answer 3:

I had similar issue and issue was not in code but was in httpd file

  1. Open the Apache configuration file located at/etc/httpd/conf/httpd.conf (or apache.conf for ubuntu)
  2. Change AllowOverride None to AllowOverride All inside the DocumentRoot Directory
    Directive, normally

Answer 4:

my solution to this problem, wich always works in my case;

create a .htaccess file in the subdirectory and add the code wich is generated when you change the permalinks on the bottom of the page.

example of the generated code;

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /kimco/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /kimco/index.php [L]
</IfModule>

References