Wordpress templating question for custom routing and arguments

Hi Everyone.

I was wondering if anyone has faced this issue before. I want to setup my own routing for wordpress. I have several templates assigned to pages (The ones with 'Template Name' parameter).

So for example, i have hierarchy like this:

Design
Design/Edit
Design/View

i have assigned each of the following a template. Now i want to pass parameters to them like:

Design/Edit/{ID}
Design/View/{ID}

and still load the same template assigned to Edit or View.

Any solution for this?

I have tried adding a rewrite rule but am not sure what to pass as pagename because my pages are all children or sub-children etc.

  • aristath
    • Recruit

    Hello there @Mindblaze, I hope you're well today!

    You'll have to create new custom template files in your theme.
    These will have to be named page-{id}.php (replacing {id} with the actual page ID.
    That page will then use the specific template file, and you can code them based on the template file that already exists in your theme.

    I hope that helps!

    Cheers,
    Ari.

  • Mindblaze
    • The Incredible Code Injector

    Okay here is what does it:

    function my_routes( $rules ){

    $wp_rules = array();
    $wp_rules['user/designs/edit/([^\/]+)/?$'] = 'index.php?pagename=user/designs/edit&my_id=$matches[1]';
    $wp_rules['products/detail/([^\/]+)/?$'] = 'index.php?pagename=products/detail&my_id=$matches[1]';

    /* Put on top of the array */
    return array_merge($wp_rules, $rules);
    }
    add_filter( 'rewrite_rules_array', 'my_routes' );

  • Hoang Ngo
    • Code Slayer

    Hi @Mindblaze,

    I hope you are well today and I'm sorry about the delay.

    For query page by pagename param, you can use the slug of the child page, which still fine.

    And then, you can use the filter "template_include", to check the current page is valid for your custom template, and return the template for that page.

    If you have any issues please don't hesitate to let us know so we can assist

    Best Regards
    Hoang

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.