tags:

views:

158

answers:

3

I have the following code to build a custom taxonomy for my portfolio:

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

function create_pc_db_taxonomies() {
    register_taxonomy( 'type', 'post', array( 'hierarchical' => true, 'label' => 'Type', 'query_var' => true, 'rewrite' => array( 'slug' => 'type' ) ) );
}

I have created a portfolio category on my site (I removed the /category/ base) and have created some items and set the custom taxonomies against them. So I get the following setup: http://domain.com/portfolio/item1/ but what I want is for the taxonomy links to look like this: http://domain.com/portfolio/type/web and then this will show a list of portfolio items related to the that type. At the moment they are root like http://domain.com/type/web these create 404's, but I have also tried adding 'portfolio/type' as the taxonomy slug but it just creates a 404 as well, but i'm pretty sure this is the wrong way of doing it anyways. Any help?

Thanks

EDIT: The site is here: http://driz.co.uk/ all the work is in a category called Portfolio and under each pic is the title and custom taxonomy (if you click on them you will get the 404)

A: 

I'm sure you can fake a slug like that, using portfolio/type - the key is, once you've updated your code, you'll need to flush the rewrite rules.

Do this by simply re-saving your permalink settings in the admin.

TheDeadMedic
Tried that but I still get 404 the site is here http://driz.co.uk/ try clicking the small links under each portfolio item title.
Cameron
Could you run this code somewhere in a theme file and let me know what you get? Just post what's relevant.`<pre><?php global $wp_rewrite; print_r($wp_rewrite->rules); ?></pre>`
TheDeadMedic
Ooh, just noticed, try taking out the `query_var` key from the argument array (flush rules again too).
TheDeadMedic
Cameron
tried removing the query_var no changes
Cameron
Okay, there's definitely a problem, since those rewrites should not be mapping to the `tag` query var.I'm gonna post another answer with a solution.
TheDeadMedic
A: 

How did you remove the category base? In .htaccess and with a rule in Dashboard/ Settings/Permalinks? Or with a plugin like WordPress › WP No Category Base « WordPress Plugins?

songdogtech
Using a plugin.
Cameron
But I tried it without the plugin to see if it was breaking it and it still threw the 404 so the no category base isn't the culprit.
Cameron
I'm sure the category base plays no part with custom tax permalinks. Custom tax URLs are of the form `siteurl.com/taxonomy/term_slug`
TheDeadMedic
A: 

Make sure all plugins are disabled (at least ones that tweak with rewrites), and delete the option rewrite_rules in your wp_options table.

Now remove any hooks in your theme that meddle with custom taxes and post types, and run this on init;

register_taxonomy(
    'type',
    'post',
    array(
        'hierarchical' => true,
        'label' => 'Type',
        'rewrite' => array('slug' => 'portfolio/type')
    )
);

In your permalink settings, choose one of the default options, and make sure both a category and tag base is set, then update.

Dump $wp_rewrite as you did before, and you should see a few lines like so;

[portfolio/type/([^/]+)/page/?([0-9]{1,})/?$] => index.php?type=$matches[1]&paged=$matches[2]
[portfolio/type/([^/]+)/?$] => index.php?type=$matches[1]

These should be near the very beginning of the array, before tags, categories and the like.

Let me know how you get on!

(When I run the code in my install of WP 3.0-beta-2 it works, so I assume something funky is happening at the moment).

TheDeadMedic
I didn't dump anything from the DB, but swapped the reg code and all my old taxonomy disappeared clearly mean WP sees this as different not sure why? But the URLs now work. What has changed in the code?
Cameron
Would you mind zipping your functions.php file (or at least the relevant code as you had it before the changes) for download? That way we could get a better idea of what was going on, but without you posting reams of code here!
TheDeadMedic
Seems WP got confused because I had named as tag and thought it was the built in tags. Very weird. All seems to be working apart from the pagination http://driz.co.uk/portfolio/category/development/page/2 any ideas? Thanks
Cameron
So are you just using WP's built in 'category' taxonomy now? Otherwise where is 'category' coming from in the URL?
TheDeadMedic
No I have set the slug as 'portfolio/category' for the custom taxonomy. But I've noticed the pagination doesn't work for anything. http://driz.co.uk/portfolio/page/2 doesn't work either. Not sure why, I've flushed the permalinks but still broken?
Cameron
I think you'll find that putting `category` in your tax slug is going to cause problems, because it's also a reserved query var in WP - what happened to `type`?
TheDeadMedic
Changing it to something else like 'type' still creates the problem.
Cameron
I'm running out of things to suggest! To be honest, without being able to look at your code and/or WP install I'm not going to be able to help any further. If I get the chance I'll try and duplicate the set-up on my localhost and run some debugging.
TheDeadMedic