TransWikia.com

Custom permalink variable on single post

WordPress Development Asked by quyet on October 20, 2020

I’m trying rewrite url for permalink on single post
my permalink: /%postname%/
My single form: http://example.com/abc-def/?chapter=1
adc-def mean: %postname%

i want permalink is: http://example.com/abc-def/chapter/1
But it is not possible.
this is my code:

add_action( 'init', 'myblog_rewrites_init' );
function myblog_rewrites_init(){
    add_rewrite_rule(
        '([^/]+)/([0-9]+)/?$',
        'index.php?name=$matches[1]&chapter=$matches[2]',
        'top' );
}

add_filter( 'query_vars', 'myblog_rewrite' );
function myblog_rewrite( $query_vars ){
    $query_vars[] = 'chapter';
    return $query_vars;
}

Please help me! Thanks every body!

One Answer

Use add_rewrite_endpoint instead of adding a new rewrite rule:

function wpa_chapter_endpoint(){
    add_rewrite_endpoint( 'chapter', EP_PERMALINK );
}
add_action( 'init', 'wpa_chapter_endpoint' );

Answered by Milo on October 20, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP