Hi Jim and thank you for your reply.
I tryed Custom Post Type Permalinks plugin, but it doesn’t allowed me to do what I need. I mean to have hierarchical custom post type without hierarchical permalinks.
I have custom post type “reviews”
I created these posts in this “reviews” post type:
– home appliances
– laundry
– washing machines
– AEG Lavamat 85470SL
Bacause of hierarchical structure, the URL of “AEG Lavamat 85470SL” post is
https://www.domain.com/reviews/home-appliances/laundry/washing-machines/aeg-lavamat-85470sl/
I need that this URL will look like this:
https://www.domain.com/reviews/aeg-lavamat-85470sl/
But there will still be visible hierarchical structure in breadcrumb navigation.
I spent all Saturday looking for a solution.
And right now I am able to have this short URL
https://www.domain.com/reviews/aeg-lavamat-85470sl/
by using this code in my functions.php file:
function reviews() {
add_rewrite_rule(
‘^reviews/([^/]+)/?$’,
‘index.php?post_type=reviews&name=$matches[1]’,
‘top’
);
}
add_action( ‘init’, ‘reviews’, 0 );
function bvt_reviews_flatten_hierarchies( $post_link, $post ) {
if ( ‘reviews’ != $post->post_type ) {
return $post_link;
}
$uri = ”;
foreach ( $post->ancestors as $parent ) {
$uri = get_post( $parent )->post_name . “/” . $uri;
}
return str_replace( $uri, ”, $post_link );
}
add_filter( ‘post_type_link’, ‘bvt_reviews_flatten_hierarchies’, 10, 2 );
But problem with this solution is that both of URLs exists on my websites.
Short URL:
https://www.domain.com/reviews/aeg-lavamat-85470sl/
and also long URL:
https://www.domain.com/reviews/home-appliances/laundry/washing-machines/aeg-lavamat-85470sl/
And it is not good for SEO to have 2 URLs for one page.
I need some rewriting rule that automatically redirect from long URL https://www.domain.com/reviews/home-appliances/laundry/washing-machines/aeg-lavamat-85470sl/
to short URL
https://www.domain.com/reviews/aeg-lavamat-85470sl/
I know I can use some redirect plugin and create such redirect by hand. But it need to rewrite automatically because I will have thousands posts.
I am not programmer and I can not figure it out such rewriting rule by myself.
If you could help me with such rewriting rule, I would be very gratefull.
Thank you for your time, Jim.
David
-
This reply was modified 7 years ago by davidki.