Complicated Permalink Structure
-
I have read alot of articles and done quiet a bit of filtering. I can accomplish parts of my requirement with any given filter or plugin, but when I combine them, I do not succeed. I need to:
- Remove Parent level category from slug. This works fine, and also works with CPTs with a modification.
add_filter ( 'post_link', 'remove_parent_cats_from_link', 10, 3 ); function remove_parent_cats_from_link( $permalink, $post, $leavename ) { $cats = get_the_category ( $post->ID ); if ( $cats ) { usort ( $cats, '_usort_terms_by_ID' ); // order by ID $category = $cats[ 0 ]->slug; if ( $parent = $cats[ 0 ]->parent ) { $parentcats = get_category_parents ( $parent, false, '/', true ); $permalink = str_replace ( $parentcats, '', $permalink ); } } return $permalink; }
- Add /%category%/ with removed parent to 3 custom post types. I added Custom Post Type Permalinks plugin and it works great with the /%category%/ for each CPT. It even picks up the filter to remove the parents with this
`add_filter ( ‘post_type_link’, ‘remove_parent_cats_from_cpt’, 10, 3 );
function remove_parent_cats_from_cpt( $permalink, $post, $leavename ) {if ( ( ‘hmk_recipe’ == get_post_type ( $post ) ) || ( ‘hmk_craft’ == get_post_type ( $post ) ) || ( ‘hmk_article’ == get_post_type ( $post ) ) ) {
$cats = get_the_category ( $post->ID );
if ( $cats ) {
usort ( $cats, ‘_usort_terms_by_ID’ ); // order by ID
$category = $cats[ 0 ]->slug;
if ( $parent = $cats[ 0 ]->parent ) {
$parentcats = get_category_parents ( $parent, false, ‘/’, true );
$permalink = str_replace ( $parentcats, ”, $permalink );
}
}}
return $permalink;
}` - Remove the /custom-post-type/ base from those 3 types of posts too. When I filter to remove that from the url, I get 400 errors. Here’s the code.
function hmk_remove_cpt_slug_craft( $post_link, $post, $leavename ) { if ( 'hmk_craft' != $post->post_type || 'publish' != $post->post_status ) { return $post_link; } $post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link ); // $post_link = str_replace( '/' . $post->post_type . '/', '/%category%/', $post_link ); return $post_link; } add_filter( 'post_type_link', 'hmk_remove_cpt_slug_craft', 10, 3 ); function hmk_parse_request( $query ) { if ( ! $query->is_main_query() || 2 != count( $query->query ) || ! isset( $query->query['page'] ) ) { return; } if ( ! empty( $query->query['name'] ) ) { $query->set( 'post_type', array( 'post', 'hmk_article', 'hmk_craft', 'page' ) ); } } add_action( 'pre_get_posts', 'hmk_parse_request' );
I feel like I’m doing too much filtering on these permalinks, and because I have used the plugin to add the /%category%/, I wonder if that’s not causing a conflict with my filters.
Also for the CPTs I have tried turning off the plugin and just adding /%category%/ to my filters. You may see that commented out. No luck.
Also I tried adding /%category%/ in the $rewrite options when I create the CPTs. Still no luck.
I am pretty green with this Rewrite API, so your help or advice is greatly appreciated.
- Remove Parent level category from slug. This works fine, and also works with CPTs with a modification.
- The topic ‘Complicated Permalink Structure’ is closed to new replies.