I did folowed the steps / solution from Jeff it helped me alot with he’s settings, got it for about 90% working properly. But getting stuck on the post / postname. It loads now the producten/%taxonomy_products%/ without the 404 errors. But when i click further on a post i get the 404 errors again.
Maybe you can see what i’am doing wrong?
Custom post type and taxonomy settings ‘Sorry for the dutch words’ :
add_action( 'init', 'products_taxonomy_options' );
function products_taxonomy_options() {
//=============== Register custom post type - PRODUCTS ================
register_post_type('products',
array( 'label' => __('Producten'), // Postype
'labels' => array( 'name' => __('Producten'),
'singular_name' => __('Producten'),
'add_new' => __('Product toevoegen'),
'add_new_item' => __('Nieuw product'),
'edit_item' => __('Wijzig product'),
'new_item' => __('Nieuw product'),
'all_items' => __('Alle producten'),
'view_item' => __('Bekijk producten'),
'search_items' => __('Zoek producten'),
'not_found' => __('Er zijn geen producten gevonden'),
'not_found_in_trash' => __('Geen producten gevonden in de prullenbak')
),
'public' => true,
'can_export' => true,
'show_ui' => true, // UI in admin panel
'_builtin' => false, // It's a custom post type, not built in
'_edit_link' => 'post.php?post=%d',
'capability_type' => 'post',
//'menu_icon' => get_bloginfo('template_url').'/images/favicon.ico',
'hierarchical' => true,
'rewrite' => array( "slug" => "producten"), // Permalinks
'supports' => array( 'title',
'editor',
'thumbnail',
),
'show_in_menu' => true,
'taxonomies' => array('productcat')
)
);
//=============== Register custom taxonomy - PRODUCTS CATEGORY ================
register_taxonomy( "productcat",
array( "products" ),
array ( "hierarchical" => true,
"label" => "Product categorieën",
'labels' => array( 'name' => __('Product categorieën'),
'singular_name' => __('Product categorie'),
'search_items' => __('Categorieën zoeken'),
'popular_items' => __('Populaire categorieën'),
'all_items' => __('Alle categorieën'),
'parent_item' => __('Huidige categorie'),
'parent_item_colon' => __('Huidige categorie:'),
'edit_item' => __('Wijzig categorie'),
'update_item' => __('Update categorie'),
'add_new_item' => __('Nieuwe categorie toevoegen'),
'new_item_name' => __('Nieuwe categorie')
),
'show_ui' => false,
'query_var' => true,
'_builtin' => false,
'paged' => true,
'rewrite' => array( 'hierarchical' => true,
'slug' =>'products',
'with_front' => false
),
)
);
}
Jeff’s settings:
add_filter('rewrite_rules_array', 'mmp_rewrite_rules');
function mmp_rewrite_rules($rules) {
$newRules = array();
$newRules['producten/(.+)/(.+)/(.+)/(.+)/?$'] = 'index.php?products=$matches[4]'; // my custom structure will also have the post name as the 5th uri segment
$newRules['producten/(.+)/?$'] = 'index.php?productcat=$matches[1]';
return array_merge($newRules, $rules);
}
function filter_post_type_link($link, $post)
{
if ($post->post_type != 'products')
return $link;
if ($cats = get_the_terms($post->ID, 'productcat'))
{
$link = str_replace('%taxonomy_products%', get_taxonomy_parents(array_pop($cats)->term_id, 'productcat', false, '/', true), $link); // see custom function defined below
}
return $link;
}
add_filter('post_type_link', 'filter_post_type_link', 10, 2);
// my own function to do what get_category_parents does for other taxonomies
function get_taxonomy_parents($id, $taxonomy, $link = false, $separator = '/', $nicename = false, $visited = array()) {
$chain = '';
$parent = &get_term($id, $taxonomy);
if (is_wp_error($parent)) {
return $parent;
}
if ($nicename)
$name = $parent -> slug;
else
$name = $parent -> name;
if ($parent -> parent && ($parent -> parent != $parent -> term_id) && !in_array($parent -> parent, $visited)) {
$visited[] = $parent -> parent;
$chain .= get_taxonomy_parents($parent -> parent, $taxonomy, $link, $separator, $nicename, $visited);
}
if ($link) {
// nothing, can't get this working :(
} else
$chain .= $name . $separator;
return $chain;
}
Permalink settings:
/producten/%taxonomy_products%/%postname%/
Working with the Taxonomic SEO Permalink plugin
Thanks!
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Thanks for referencing my solution. You forgot one part though – defining the rewrite of your cpt. From my solution:
First get your slugs right when defining your custom post types and
taxonomies: for the custom post type it should be
basename/%taxonomy_name%and the slug for your taxonomy should be
just basename. Dont forget to also add ‘hierarchical’ => true to the
taxonomy rewrite array to get nested terms in your url. Also make sure
query_var is set to true in both cases.
So in your case your rewrite for your custom post type should be producten/%productcat%. Also it looks like in my filter_post_type_link function you need to change %taxonomy_products% to %productcat%.
Hopefully that will make it work!
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0