WordPress Development Asked by Kom on November 26, 2021
i have a page call “city”
http://localhost/wordpress/city/
and it get details from mysql by url id like
http://localhost/wordpress/city/?id=paris
and i want to change it like
http://localhost/wordpress/city/paris
and the real link of city page in wordpress is
http://localhost/wordpress/index.php?p=5
I am trying do add RewriteRule in htaccess and it doesn’t work
also i try add_rewrite_rule
and it doesn’t work
add_action( 'init', 'add_mypage_rule' );
function add_mypage_rule(){
add_rewrite_tag("%id%", '(d+)');
add_rewrite_rule('^city/([^/]*)/?','index.php?p=5&id=$matches[1]','top');
}
If city
belongs to the page
post type, then your rule should either be:
add_rewrite_rule(
'^city/([^/]*)/?',
'index.php?pagename=city&id=$matches[1]',
'top'
);
or:
add_rewrite_rule(
'^city/([^/]*)/?',
'index.php?page_id=5&id=$matches[1]',
'top'
);
Using p
, the query will assume post type is post
, not page
.
Also, if your code uses $_GET['id']
to fetch the value, this will no longer work with a rewrite rule. You will need to use get_query_var('id')
instead. If you don't have access to the code, you can set it in an action that fires before the code tries to get that value:
function wpd_set_id() {
if( false !== get_query_var( 'id', false ) ){
$_GET['id'] = get_query_var( 'id' );
}
}
add_action( 'parse_query', 'wpd_set_id' );
I also suggest using a more unique query var than id
!
Answered by Milo on November 26, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP