I have created hierarchy based custom permalinks with the help of this link, but now it is causing a 404 error on another custom taxonomy used in the site. I want custom permalinks on this other taxonomy also, but if I don't use any rewrite arguments and leave it on its default permalink, it is still not working.
My code for the second taxonomy:
register_post_type(
'gallery',
array(
'public' => true,
'menu_position' => 15,
'supports' => array( 'title', 'editor', 'comments', 'thumbnail' ),
'taxonomies' => array( '' ),
'menu_icon' => get_template_directory_uri() . '/img/icon_gallery.png',
'register_meta_box_cb' => 'add_gallery_metaboxes'
)
);
register_taxonomy(
'gallery_category',
'gallery',
array(
'hierarchical' => true,
'labels' => $labels
)
);
And here is my first custom taxonomy code:
register_post_type(
'article',
array(
'public' => true,
'hierarchical' => true,
'menu_position' => 15,
'supports' => array( 'title', 'editor', 'comments', 'thumbnail' ),
'taxonomies' => array( '' ),
'menu_icon' => get_template_directory_uri() . '/img/icon_article.png',
'query_var' => true,
'rewrite' => array(
'slug' => '%article_category%/articles',
'with_front' => true
),
'has_archive' => '%article_category%',
'register_meta_box_cb' => 'add_article_metaboxes'
)
);
register_taxonomy(
'article_category',
'article',
array(
'hierarchical' => true,
'labels' => $labels,
'query_var' => true,
'rewrite' => array( 'slug' => '', 'hierarchical' => true ),
)
);
Function for custom permalink:
function filter_post_type_link( $link, $post ) {
if ( $post->post_type != 'article' )
return $link;
if ( $cats = get_the_terms( $post->ID, 'article_category' ) ) {
$link = str_replace( '%article_category%', get_taxonomy_parents( array_pop( $cats )->term_id,
'article_category', false, '/', true ), $link ); // See custom function defined below
}
return $link;
}
add_filter( 'post_type_link', 'filter_post_type_link', 10, 2 );