Answer the question
In order to leave comments, you need to log in
Why doesn't URL rewriting work in WordPress?
Good afternoon!
There is a function that rewrites URLs:
from: site.ru/product/salat_cezar
to: site.ru/463
So, only the last rewrite rule (product_3) works in it. Why don't the first two rules (product and product_2) work? Why only the last rule works and the first two do not work?
The function itself:
function fun_798668( $link, $post = 0 ) {
if ( $post->post_type == 'product' ) {
return home_url( '/' . $post->ID );
} elseif ( $post->post_type == 'product_2' ) {
return home_url( '/' . $post->ID );
} elseif ( $post->post_type == 'product_3' ) {
return home_url( '/' . $post->ID );
} else {
return $link;
}
}
add_filter( 'post_type_link', 'fun_798668', 1, 3 );
function fun_798686() {
add_rewrite_rule(
'([0-9]+)?$',
'index.php?post_type=product&p=$matches[1]',
'top'
);
add_rewrite_rule(
'([0-9]+)?$',
'index.php?post_type=product_2&p=$matches[1]',
'top'
);
add_rewrite_rule(
'([0-9]+)?$',
'index.php?post_type=product_3&p=$matches[1]',
'top'
);
}
add_action( 'init', 'fun_798686' );
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question