1

My parent page: http://example.com/id

Virtual pages are:

  • http://example.com/id/1563
  • http://example.com/id/john/5896
  • http://example.com/id/jack-miami/3054

I use shortcode in parent page that gets id number from URL then show the result.

Now all above pages show 404 errors. Is it possible to use the parent page for showing all pages with links containing example.com/id/

I used this but it does not work:

function edit_the_permalink($url){
    if(strpos($url, get_site_url().'/id/') !== false){
        $url = get_site_url().'/id/';
    }
    return $url;
}
add_filter('the_permalink', 'edit_the_permalink');
0

add_filter('the_permalink'), is the wrong tool for the job, it gets you the full permalink for the current post or a specific post ID.

It seems like the proper approach would be to use a rewrite rule:

add_filter('query_vars', function($vars){
      $vars[] = 'id';
      return $vars;
});
add_rewrite_rule('id\/([\w\-]+)\/?', 'index.php?id=$matches[1]', 'top');

CAVEAT: I haven't tried this, and id might be a reserved name in the rewrite rule.

But, this is the general idea for getting virtual subpages from a real parent page.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.