0

I have an artists-single.php page template which shows when I navigate to:

/artists/djave_co

Is there a way to route

/artists/djave_co/anything

to the same artists-single.php page? Then I can use some PHP logic to deal with the rendering of the page.

1 Answer 1

1

I usually struggle with this, however this time I managed it relatively easy. Main reference for this answer is here:

https://codex.wordpress.org/Rewrite_API/add_rewrite_rule

I'd like to use anything in the below as discipline.

/artists/djave_co/anything

First of all, I added a "rewrite tag"

function custom_rewrite_tag() {
  add_rewrite_tag('%discipline%', '([^&]+)');
}
add_action('init', 'custom_rewrite_tag', 10, 0);

Next, I did print_r($wp_query->query_vars) on the default custom post page. This resulted in the below:

Array
(
    [page] => 0
    [artists] => djave_co
    [post_type] => artists
    [name] => djave_co
    [error] => 
    ...

This helped me, as I could see that I'd need to set post_type=artists&artists=djave_co in the next step.

The next step was to add a custom rewrite rule:

function custom_rewrite_rule() {
  add_rewrite_rule(
    '^artists/([^/]*)/([^/]*)/?',
    'index.php?post_type=artists&artists=$matches[1]&discipline=$matches[2]',
    'top');
}
add_action('init', 'custom_rewrite_rule', 10, 0);

The final and very important step was to clear the permalinks in the WordPress admin panel.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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