March 5, 201115 yr So, I've been banging my head against this problem all day so I thought I'd throw it out there and see if anyone else can point me at a good resource. I've created a custom post type "portfolio" that takes two custom taxonomies, "portfolio-category" (hierarchical, like normal categories) and "portfolio-skills" (non-hierarchical, like tags). All this works fine. I use a custom permalink structure of /%category%/%postname% for my blog, and I'd like to achieve the same for my portfolio, but at the moment it's defaulting to /portfolio/%postname%/ - I've tried hooking into the rewrites every which way but I can't seem to get it to include the portfolio-category slug in the portfolio item permalink. Any ideas, folks? Anyone written a definitive guide to WordPress rewrites? PS: I don't need anyone to say "use this plugin" - I'm moving AWAY from a plugin for my portfolio and coding it all into my theme because I run too many plugins as it is.
March 5, 201115 yr Author Can you post your register_taxonomy() function? register_taxonomy( 'portfolio-category', 'portfolio', array( 'label' => 'Portfolio Categories', 'singular_label' => 'Portfolio Category', 'hierarchical' => true, 'query_var' => 'portfolio-category', 'rewrite' => true, 'public' => true, 'show_ui' => true ) ); I'm probably missing something really simple, but it's been a long day. Edited March 5, 201115 yr by Renaissance-Design
March 5, 201115 yr Try changing the rewrite line to: 'rewrite' => array('slug' => 'portfolio/category')
March 6, 201115 yr Author Tried that earlier; it affects the taxonomy slug but not the post type. Ok, got a bit further. Setting the slugs this way: Post type: 'rewrite' => array('slug' => 'portfolio/%portfolio-category%') And adding the following function: function filter_post_type_link($link, $post) { if ($post->post_type != 'portfolio') { return $link; } if ($cats = get_the_terms($post->ID, 'portfolio-category')) { $link = str_replace('%portfolio-category%', array_pop($cats)->slug, $link); return $link; } } add_filter('post_type_link', 'filter_post_type_link', 10, 2); That adds the category to the post slug, but the category and portfolio index are still Donald Ducked. Edited March 6, 201115 yr by Renaissance-Design
March 6, 201115 yr Author Right, for anyone else who has this problem, here's how I solved it. This method requires a page with the slug "portfolio" to act as a placeholder, similar to the setting to use a static front page and a blog page. add_action('init', 'portfolio_register'); add_filter('post_type_link', 'filter_post_type_link', 10, 2); function portfolio_register() { $labels = array( 'name' => _x('Portfolio', 'post type general name'), 'singular_name' => _x('Portfolio Piece', 'post type singular name'), 'add_new' => _x('Add New', 'portfolio piece'), 'add_new_item' => __('Add New Portfolio Piece'), 'edit_item' => __('Edit Portfolio Piece'), 'new_item' => __('New Portfolio Piece'), 'view_item' => __('View Portfolio Piece'), 'search_items' => __('Search Portfolio'), '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() . '/images/portfolio.png', 'rewrite' => array('slug' => 'portfolio/%portfolio-category%'), 'capability_type' => 'post', 'hierarchical' => false, 'menu_position' => null, 'show_in_nav_menus' => false, 'supports' => array('thumbnail','title','editor') ); register_post_type( 'portfolio' , $args ); } register_taxonomy( 'portfolio-category', 'portfolio', array( 'label' => 'Portfolio Categories', 'singular_label' => 'Portfolio Category', 'hierarchical' => true, 'query_var' => 'portfolio-category', 'rewrite' => array('slug' => 'portfolio'), 'public' => true, 'show_ui' => true ) ); function filter_post_type_link($link, $post) { if ($post->post_type != 'portfolio') { return $link; } if ($cats = get_the_terms($post->ID, 'portfolio-category')) { $link = str_replace('%portfolio-category%', array_pop($cats)->slug, $link); return $link; } } That was a lot harder than it should have been. Edited March 6, 201115 yr by Renaissance-Design
March 6, 201115 yr Author I was dog-tired yesterday. Kept making really stupid errors and not noticing for half an hour.
Create an account or sign in to comment