admin管理员组

文章数量:1323502

I have a CPT called jobs and a page named Job Apply. I am trying to achieve something like below url:


Where, job-slug can be any job. i.e Programmer, Developer etc..

On clicking job's apply button, it should open an apply page which consists a simple form, but url should be as mentioned above.

I am trying to achieve it by using add_rewrite_rule(), but after trying too much, it is still redirecting me on the below url:


Any help would be appreciated. Thanks in advance.

I have a CPT called jobs and a page named Job Apply. I am trying to achieve something like below url:

http://example/job-slug/apply

Where, job-slug can be any job. i.e Programmer, Developer etc..

On clicking job's apply button, it should open an apply page which consists a simple form, but url should be as mentioned above.

I am trying to achieve it by using add_rewrite_rule(), but after trying too much, it is still redirecting me on the below url:

http://example/apply

Any help would be appreciated. Thanks in advance.

Share Improve this question edited Aug 29, 2020 at 17:26 Vishal Bhatt asked Aug 29, 2020 at 15:02 Vishal BhattVishal Bhatt 1413 bronze badges 1
  • did you try changing custom post type slug first? – Jignesh Bhavani Commented Aug 29, 2020 at 15:08
Add a comment  | 

2 Answers 2

Reset to default 1

I assume you have a custom taxonomy for your CPT, so your CPT should be 'job' and your custom taxonomy can then be 'jobs' and then you'd have custom terms within that taxonomy for each job type. The Posts then would consist of the individual job with any associated relevant data (rate of pay, hours, etc).

If that is your setup - and how I would do it - then if you DON'T want the CPT or Taxonomy base in the URL, you do have to do a re-write (see this page for some more helpful advice).

Remove Custom Taxonomy Base

I think the problem you're having is that you're using a Page (Apply) to hold your form and related job data - which is why your re-write isn't working.

Try changing to using a Template that is applied to all posts within the CPT, and put your form (either embedded or with a PHP call to the form) on your Template, using it to also pull in any custom metadata through a loop of your CPT and taxonomy.

If you want to keep your current structure I would use a routing library.
With this library you could solve your issue like this:

Routes::map(':job_slug/apply', function($url_params) {
    $params = [];
    $wp_query_args = [
        'post_type' => 'page',
        'posts_per_page' => 1,
        'post_name__in' => ['apply']
    ];

    Routes::load('page.php', $params, $wp_query_args);
});

With $url_params['job_slug'] you are able to access the current job slug with which you could query more information and then pass those via the second parameter of the load method to your template.

The first parameter simply tells it to use the page.php template and the third parameter tells WordPress wich query to execute when reaching this route. In this case it will simply load the Job Apply page.

本文标签: url rewritingHow to change default page slug