Been trying to re-map the following
/test-sale-yacht/comfortably-numb-262749/
/test-sale-yacht/?yacht_name_id=comfortably-numb-262749
After some testing in a regular expression tester I've put the following command in the init routine of the new plugin
add_rewrite_rule('test-sale-yacht/([^/]*)?', 'index.php?name=test-sale-yacht&yacht_name_id=$1', 'top');
This looks similar to others so i figure it should work
I reset the permalinks several times by going to settings > permalinks and hitting save still does not work.
Tried putting it in functions.php
in the theme, still no soap.
According to a debug plugin there are no rules getting hits on the page.
tried /test-sale-yacht/?yacht_name_id=comfortably-numb-262749
, that works
tried /?name=test-sale-yacht&yacht_name_id=comfortably-numb-262749
, that works
but /test-sale-yacht/comfortably-numb-262749/
, nothing.
Totally foxed on this.
Jules
Been trying to re-map the following
/test-sale-yacht/comfortably-numb-262749/
/test-sale-yacht/?yacht_name_id=comfortably-numb-262749
After some testing in a regular expression tester I've put the following command in the init routine of the new plugin
add_rewrite_rule('test-sale-yacht/([^/]*)?', 'index.php?name=test-sale-yacht&yacht_name_id=$1', 'top');
This looks similar to others so i figure it should work
I reset the permalinks several times by going to settings > permalinks and hitting save still does not work.
Tried putting it in functions.php
in the theme, still no soap.
According to a debug plugin there are no rules getting hits on the page.
tried /test-sale-yacht/?yacht_name_id=comfortably-numb-262749
, that works
tried /?name=test-sale-yacht&yacht_name_id=comfortably-numb-262749
, that works
but /test-sale-yacht/comfortably-numb-262749/
, nothing.
Totally foxed on this.
Jules
Share Improve this question edited Jul 8, 2020 at 6:01 nmr 4,5672 gold badges17 silver badges25 bronze badges asked Jul 7, 2020 at 14:13 JulesJules 111 bronze badge 7 | Show 2 more comments2 Answers
Reset to default 1You're missing the WP $matches[1]
syntax for the regex that gets matched.
I think you want /?
on the end too.
Easiest way I found to test was to temporarily put flush_rewrite_rules()
in whilst testing, then you don't have to do anything else. I have a similar regex which sends /foo/catname/
to index.php?taxonomy=category&term=catname
, here's what I used to develop on a clean test WP install - this is working fine for me.
function foobar() {
add_rewrite_rule('^foo/([^/]+)/?', 'index.php?taxonomy=category&term=$matches[1]', 'top');
flush_rewrite_rules();
}
add_action('init', 'foobar', 10, 0);
So you might want to try:
add_rewrite_rule('test-sale-yacht/([^/]+)/?', 'index.php?name=test-sale-yacht&yacht_name_id=$matches[1]', 'top');
Edit: As per comment by @Tom J Nowell, flush_rewrite_rules is an expensive call - my suggestion to do this only on a dev or very low traffic site.
This worked in the end, not sure why I was not getting anything showing up in the re-write rules initially but this worked.
add_rewrite_rule( 'test-sale-yacht/([^/]*)?',
'index.php?pagename=test-sale-yacht&yacht_name_id=$matches[1]', 'top');
with yacht_name_id
added in as a filter
Jules
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742281102a4414493.html
add_rewrite_rule
with no context, I can't tell how it's being called or when or on what hook – Tom J Nowell ♦ Commented Jul 7, 2020 at 15:26