1

I have created page-images.php, inside that file I have some code with Template name, in dashboard I have created page Slike and chose that template... all that is working.

I have added rewrite rule just as it is described here and I have flushed permalinks https://codex.wordpress.org/Rewrite_API/add_rewrite_rule

function pl_add_rewrite_rule() {
  add_rewrite_rule('^slike/([^/]*)/([^/]*)/?','index.php?page_id=45893&carstvo=$matches[1]&kategorija=$matches[2]','top');
}
add_action('init', 'pl_add_rewrite_rule', 10, 0);

Now these urls work:

  • localhost/mysite/slike
  • localhost/mysite/slike/biljke/cvijece

But this doesn't

  • localhost/mysite/slike/biljke

In Codex it says that capture groups are optional. Do I have to do something else to make it work? I want to have all 3 kinds of urls working

$regex
(string) (required) A regular expression to match against a requested URL.
 This can optionally use one or more capture groups.
1
  • 1
    what is optional is the use of capture groups not the catching in the regex. you can us this website to construct the regex you need : regex101.com
    – mmm
    Sep 16 '16 at 22:10
1

Ok after 4 hours I managed to make it work

function pl_add_rewrite_rule() {
    // this works with pagination too
    add_rewrite_rule('^slike(\/([a-z]+))?(\/([a-z]+))?(\/page\/([0-9]+))?$','index.php?page_id=45893&carstvo=$matches[2]&kategorija=$matches[4]&paged=$matches[6]','top');

    add_rewrite_tag('%carstvo%','([^/]*)');
    add_rewrite_tag('%kategorija%','([^/]*)');
}
add_action('init', 'pl_add_rewrite_rule', 10, 0);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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