generate_rewrite_rules
-
I use polylang pro for different websites.
I have a cpt called “machine” and a custom taxonomy called “machine_type”.
I need to rewrite the url of the machines.
The permalink structure should look like “machine/machine_type/title”,
where machine is the translated slug of the posttype, machine_type ist the translated slug of the taxonomy and title is the post_title.How can I implement this?
function rewritePermalink($permalink, $post) { if($post->post_type == 'machine' ) { $terms = get_the_terms( $post, 'machine_type' ); $termSlug = ''; if( ! empty( $terms ) ) { foreach ( $terms as $term ) { $termSlug = $term->slug; break; } } $permalink = get_home_url() ."/machine/" . $termSlug . '/' . $post->post_name; //$permalink = get_home_url() ."/machine/test/" . $post->post_name; } return $permalink; } add_filter('post_type_link', "rewritePermalink", 10, 2); function rewriteRules($wp_rewrite) { $rules = array(); $terms = get_terms( array( 'taxonomy' => 'machine_type', 'hide_empty' => false, ) ); $postType = 'machine'; foreach($terms as $term) { $rules['machine/' . $term->slug . '/([^/]*)$'] = 'index.php?post_type=' . $postType. '&machine=$matches[1]&name=$matches[1]'; } $wp_rewrite->rules = $rules + $wp_rewrite->rules; } add_filter('generate_rewrite_rules', 'rewriteRules');
Without Polylang it works like a charm.
Hope that anybody can help ?? .Greetings
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
- The topic ‘generate_rewrite_rules’ is closed to new replies.