最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

permalinks - Process all virtual sub pages on parent page

programmeradmin1浏览0评论

My parent page:

Virtual pages are:

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/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');

My parent page: http://example/id

Virtual pages are:

  • http://example/id/1563
  • http://example/id/john/5896
  • http://example/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/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');
Share Improve this question edited Feb 4, 2016 at 10:19 Sven 3,6841 gold badge35 silver badges48 bronze badges asked Feb 4, 2016 at 9:13 user2511140user2511140 1515 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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.

发布评论

评论列表(0)

  1. 暂无评论