Home > Back-end >  add_rewrite_rule() issue with parent/child/child page
add_rewrite_rule() issue with parent/child/child page

Time:10-16

I have a parent/child and another parent/child/child pages. I have 2 separate page templates for both pages and the following add_rewrite_rule() code in place:

add_rewrite_rule( '^user/dashboard/([^/]*)/?’, 'index.php?pagename=user/dashboard&profile=$matches[1]', 'top' );

add_rewrite_rule( '^user/dashboard/billing/([^/]*)/?’, 'index.php?pagename='user/dashboard/billing&profile=$matches[1]', 'top' );

The first one (i.e. parent/child) works fine but the 2nd one (i.e parent/child/child) is not working. Meaning when I go to the parent/child/child page, it is using the template of the parent/child page and not the one assigned to itself.

If I comment out the first add_rewrite_rule() line, it works as expected. Seems like the first one is overriding the 2nd one.

I have tried flushing the rewrite rules manually by going to settings > permalinks.

I have even tried to change the order of the rewriting rules i.e. I tried registering the parent/child/child path before parent/child rule and then flushed the rules manually. But that also didn't help.

Any help is appreciated.

CodePudding user response:

You need to change the order, first comes the more specific add_rewrite_rule , then the general one, try this

   add_rewrite_rule( '^user/dashboard/billing/([^/]*)/?', 'index.php?pagename=user/dashboard/billing&profile=$matches[1]', 'top' );
   add_rewrite_rule( '^user/dashboard/([^/]*)/?', 'index.php?pagename=user/dashboard&profile=$matches[1]', 'top' );

After the change, you have to turn off/on the permalinks in the settings to clear the cache.

  • Related