Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Custom Post Permalinks WordPress

  • SOLVED

Hello,

It's annoying that hierarchical permalinks for custom taxonomies don't work out of the box for 3.0.

I've seen and tried a number of solutions. I think one of you should be able to point me in the right direction.

OK, so...

I have a post_type products.
I have a taxonomy Product-Categories. There are child categories within a category "products".
All the permalinks to my products have /products/%postname% when they should be /products/%child_cat%/%postname%/ .
For the single pages, I need to use a custom template e.g. taxonomy-Product-Categories-products-child-cat.php

I'm sure this is a quick fix.

Can you help?





/* Products */

add_action('init', 'products_register');

add_action("admin_init", "admin_init");

add_action('save_post', 'save_details_products');

add_action("manage_posts_custom_column", "products_custom_columns");

add_filter("manage_edit-products_columns", "products_edit_columns");

//register_taxonomy("Categories", array("products"), array("hierarchical" => true, "label" => "Product Categories", "singular_label" => "Product Category", "rewrite" => true));

register_taxonomy("Product-Categories", array("products"), array("hierarchical" => true, "label" => "Product Categories", "singular_label" => "Product Category", 'rewrite' => array('slug' => 'Product-Categories/category'), 'with_front' => true, 'query_var' => true));




function products_register() {



$labels = array(

'name' => _x('Products', 'post type general name'),

'singular_name' => _x('Products', 'post type singular name'),

'add_new' => _x('Add New', 'product'),

'add_new_item' => __('Add New Product'),

'edit_item' => __('Edit Product'),

'new_item' => __('New Product'),

'view_item' => __('View Product'),

'search_items' => __('Search Products'),

'not_found' => __('Nothing found'),

'not_found_in_trash' => __('Nothing found in Trash'),

'parent_item_colon' => ''

);


$args = array(

'labels' => $labels,

'public' => true,

'publicly_queryable' => true,

'show_ui' => true,

'query_var' => true,

'menu_icon' => get_stylesheet_directory_uri() . '/article16.png',

'rewrite' => true,

'capability_type' => 'post',

'hierarchical' => true,

'menu_position' => null,

'supports' => array('title','editor','thumbnail')

);



register_post_type( 'products' , $args );
flush_rewrite_rules( false );

}

//flush_rewrite_rules();

function save_details_products(){

global $post;

update_post_meta($post->ID, "product_url", $_POST["product_url"]);

}

function products_edit_columns($columns){

$columns = array(

"cb" => "<input type=\"checkbox\" />",

"title" => "Product Title",

"description" => "Description",

"categories" => "Product Categories",

);

return $columns;

}



function products_custom_columns($column){

global $post;



switch ($column) {

case "description_product":

the_excerpt();

break;

case "product_categories":

echo get_the_term_list($post->ID, 'Product Categories', '', ', ','');

break;

}

}


function credits_meta_product() {
global $post;
$custom = get_post_custom($post->ID);
$product_url = $custom["product_url"][0];

?>


<p><label>Product URL:</label><br />

<textarea cols="50" rows="5" name="product_url"><?php echo $product_url; ?></textarea></p>

<?php

}


Answers (3)

2010-10-24

Milan Petrovic answers:

I am the author of GD Custom Post Types and Taxonomies Tools plugin ( http://dv4p.com/gdtt ), and I recently got similar question on this also.

WordPress doesn't allow to mix two taxonomies in a URL as you want to do with products and categories. I am not sure if that will be supported by default in next versions, but right now something like that would require making custom rewrite rules to do it, and I wasn't successful in making that at all so far. It's too complicated to resolve properly right now using current rewriting engine WP has.


conundrum comments:

Hello,

So you are saying there is no way to rewrite to both the parent category (taxonomy) and the child category?

What about this?

http://xplus3.net/2010/05/20/wp3-custom-post-type-permalinks/

Also, is there a way to check what post you are looking at in the way that you query the posts like this? query_posts('product-categories=flexible-engineering&posts_per_page=60');


Milan Petrovic comments:

@conundrum, you can have one taxonomy in a URL, not two different taxonomies. One taxonomy can be hierarchical and it will contain all children categories in the URL. That's OK, there are limitations in combining some elements, and so far WP doesn't support all that. It's very complicated to make good rules that will resolve universally all kinds of things you can try to use with taxonomies and post types. For now stick with what WP supports and wait for changes in rewrite engine coming in WP 3.1.


Milan Petrovic comments:

Also, WP doesn't allow using taxonomies in rules for custom post types right now.


conundrum comments:

Hello,

That's exactly what I want to do, include the children categories. I'm not talking about two different taxonomies, sorry for the confusion.

So how do I include the children? e.g. products/child-category/product-name ?

2010-10-24

Navjot Singh answers:

This feature has been addressed in this [[LINK href="http://core.trac.wordpress.org/ticket/12891"]]ticket[[/LINK]] and will be possible when Wordpress 3.1 releases.

2010-10-24

Cosmin Popovici answers:

Try the [[LINK href="http://wordpress.org/extend/plugins/custom-post-permalinks/"]]Custom Post Permalinks[[/LINK]] plugin.