最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

url rewriting - Custom rewrite after the default term link URL

matteradmin5PV0评论

I have a taxonomy car and a couple terms like kia, honda, and toyota.

Using the function get_term_link(), I can get a term archive link like this:

site/car/honda/

I am trying to use custom rewrite rules to get this structure:

site/car/honda/{$trim}

My rewrite rule looks like this:

add_rewrite_rule(
    home_url() . '/car/([^/]*)/([^/]*)/?',
    'index.php?taxonomy=car&term=$matches[1]&model=$matches[1]&trim=$matches[2]',
    'top'
);

However it does not work properly. The default term archive links work (site/car/honda), but if I add anything as a $trim, WordPress redirects to a 404 which leads me to believe that my regex isn't being matched at all.

My taxonomy is non-hierarchical.

Any help would be appreciated, thank you.

I have a taxonomy car and a couple terms like kia, honda, and toyota.

Using the function get_term_link(), I can get a term archive link like this:

site/car/honda/

I am trying to use custom rewrite rules to get this structure:

site/car/honda/{$trim}

My rewrite rule looks like this:

add_rewrite_rule(
    home_url() . '/car/([^/]*)/([^/]*)/?',
    'index.php?taxonomy=car&term=$matches[1]&model=$matches[1]&trim=$matches[2]',
    'top'
);

However it does not work properly. The default term archive links work (site/car/honda), but if I add anything as a $trim, WordPress redirects to a 404 which leads me to believe that my regex isn't being matched at all.

My taxonomy is non-hierarchical.

Any help would be appreciated, thank you.

Share Improve this question asked Mar 21, 2019 at 18:37 somebodysomewheresomebodysomewhere 8102 gold badges13 silver badges21 bronze badges 0
Add a comment  | 

1 Answer 1

Reset to default 3

(Revised answer)

So Let's See The Issues With Your Rewrite Rule..

  1. The first parameter passed to add_rewrite_rule() (which is the regular expression pattern) should not start with home_url() or any functions which outputs the website URL. So this is wrong:

    home_url() . '/car/([^/]*)/([^/]*)/?'
    

    because the generated regular expression (RegEx) pattern would be:

    http://example/car/([^/]*)/([^/]*)/?
    

    which will never be matched, because the request (see WP::$request) never starts with the website URL. And it doesn't even start with /.

  2. Is the &model=$matches[1] part a typo?

    index.php?taxonomy=car&term=$matches[1]&model=$matches[1]&trim=$matches[2]
    

Now The Fixes

  1. The home_url() . '/car/([^/]*)/([^/]*)/?' should be (no trailing / at the start):

    '^car/([^/]*)/([^/]*)/?'
    
  2. The index.php?taxonomy=car&term=$matches[1]&model=$matches[1]&trim=$matches[2] should probably be without the &model=$matches[1]:

    index.php?taxonomy=car&term=$matches[1]&trim=$matches[2]
    

    However, even with that &model=$matches[1], the rule actually worked in my case.

  3. And not sure if you've already added this somewhere in your code, but I'd add it so that I could get the value of the "trim" parameter in the URL:

    add_rewrite_tag( '%trim%', '([^/]+)' );
    

    You'd add that before/after the add_rewrite_rule() line, and to get the "trim" value, you could use get_query_var( 'trim' ).

Post a comment

comment list (0)

  1. No comments so far