$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>url rewriting - Rewrite query var on postname|Programmer puzzle solving
最新消息: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 - Rewrite query var on postname

matteradmin12PV0评论

I'm trying to do a rewrite, so that the query var is going to look pretty:

This is the URL structure I got now:

example/post-type-archive/post-name/?subpage=about

This is what I want:

example/post-type-archive/post-name/about

Please note, that post_name is changing depending what site you're on. All the answers I've found so far, didn't consider that.

This is my current code:

add_filter( 'query_vars', function($vars) {
    $vars[] = 'subpage';
    return $vars;
});
add_action('init', function() {
    add_rewrite_tag('%subpage%', '([^/]+)');
    add_rewrite_rule('[^post-type-archive/]([^/]+)','index.php?name=$matches[1]&subpage=$matches[2]','top');
});

This makes the page return an 404 - page not found page.

And yes... I have flushed the rewrite rules after every changes I've made.


EDIT

I got it to work by using this:

add_action( 'init', function() {
    add_rewrite_tag('%subpage', '([^&]+)');
    add_rewrite_rule(
        '^post-type-archive/([^/]*)/([^/]*)/?',
        'index.php?post_type=post-type-archive&name=$matches[1]&subpage=$matches[2]',
        'top'
    );
});

The only problem now, is that pagination don't work anymore on post-type-archive.

I'm trying to do a rewrite, so that the query var is going to look pretty:

This is the URL structure I got now:

example/post-type-archive/post-name/?subpage=about

This is what I want:

example/post-type-archive/post-name/about

Please note, that post_name is changing depending what site you're on. All the answers I've found so far, didn't consider that.

This is my current code:

add_filter( 'query_vars', function($vars) {
    $vars[] = 'subpage';
    return $vars;
});
add_action('init', function() {
    add_rewrite_tag('%subpage%', '([^/]+)');
    add_rewrite_rule('[^post-type-archive/]([^/]+)','index.php?name=$matches[1]&subpage=$matches[2]','top');
});

This makes the page return an 404 - page not found page.

And yes... I have flushed the rewrite rules after every changes I've made.


EDIT

I got it to work by using this:

add_action( 'init', function() {
    add_rewrite_tag('%subpage', '([^&]+)');
    add_rewrite_rule(
        '^post-type-archive/([^/]*)/([^/]*)/?',
        'index.php?post_type=post-type-archive&name=$matches[1]&subpage=$matches[2]',
        'top'
    );
});

The only problem now, is that pagination don't work anymore on post-type-archive.

Share Improve this question edited Feb 27, 2019 at 22:32 Jesper asked Feb 27, 2019 at 14:14 JesperJesper 32 bronze badges 2
  • How are you using subpage in your code? If you rewrite the URL like this you need to use get_query_var( 'subpage' ). $_GET['subpage'] won't work anymore. – Jacob Peattie Commented Feb 27, 2019 at 14:19
  • Sorry. Forgot to tell that my code, returns an 404 - page not found page – Jesper Commented Feb 27, 2019 at 14:26
Add a comment  | 

1 Answer 1

Reset to default 0

So, first up, I don't believe that you need a rewrite tag in this situation, so you can omit that.

The issue, I believe, with the remaining code is that your rewrite rule is only capturing 1 match (because there's only one set of ()), so subpage isn't receiving a value. Additionally, your rule seems to exclude any posts of your post type ( because of the ^). Actually, it seems to exclude any URLs where the post name begins with any of the letters p,o,s,t,_,t,y,p,e.

I believe your rewrite rule should be:

add_rewrite_rule( 'post_type/([^/]+)/([^/]+)/?', 'index.php?name=$matches[1]&subpage=$matches[2]', 'top' );

Then you just need to make sure that in your template you're accessing the subpage value correctly. $_GET['subpage'] will no longer work. Instead you need to use get_query_var( 'subpage' ).

Also make sure you re-save your Permalinks settings.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far