I am a Chinese WordPress developer, I am not familiar with English, all the content is Machine Translation.

我是一个中国的WordPress的开发者,我不熟悉英语,所有的内容都是机器翻译。

I would like to create a separate comment page for my site.

我希望能为我的网站创建一个单独的评论显示页面

This is my idea:

我的想法如下

I created a postcomment-page.php page on my WordPress Theme

it’s URL is xxx.com/postcomment-page

I have a $_GET[‘postid’] and a $_GET[‘paged’] on this page.

A total of two get requests,

Normal URL:xxxx.com/?pagename=postcomment-page&postid={postid}&paged={paged}

I would like to URL: xxxx.com/postcomment-page/{postid}-paged-{paged}.html

So I can get to the two GET request.

But I tried various ways to use it.

Ask you to help me, thank you very much.

1 Answer
1

You need a rewrite rule! First things first let’s get that in place:

function wpse_226796_rewrite_rule( $rules ) {
    global $wp_rewrite;

    // http://php.net/manual/en/reference.pcre.pattern.syntax.php
    $regex = 'postcomment-page/([1-9][0-9]*)-paged-([1-9][0-9]*)\.html';

    $query = sprintf(
        '%s?pagename=postcomment-page&postid=%s&paged=%s',
        $wp_rewrite->index,
        $wp_rewrite->preg_index( 1 ),
        $wp_rewrite->preg_index( 2 )
    );

    // Push our rule to the top of $rules
    return array( $regex => $query ) + $rules;
}

add_filter( 'page_rewrite_rules', 'wpse_226796_rewrite_rule' );

Next we need to register our query vars so that WordPress correctly parses the “pretty” permalink into our query:

function wpse_226796_query_vars( $vars ) {
    // "paged" is already a core public query var
    $vars[] = 'postid';

    return $vars;
}

add_filter( 'query_vars', 'wpse_226796_query_vars' );

Now we need to “flush” these rules to the database cache – go to Settings > Permalinks and click Save.

Now a URL like postcomment-page/237-paged-2.html will give:

<?php echo get_query_var( 'postid' ); // 237 ?>
<?php echo get_query_var( 'paged' ); // 2 ?>

Leave a Reply

Your email address will not be published. Required fields are marked *