url rewriting - Rewrite query var on postname

admin2025-06-02  2

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.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748856893a314294.html

最新回复(0)