WP_REWRITE在第三级URL上不起作用

时间:2012-07-03 作者:Paul Dumitru

我有以下代码:

global $wp;
        $wp->add_query_var(\'akey\');
        $wp->add_query_var(\'sort\');
        $wp->add_query_var(\'paged\');
        add_rewrite_rule(\'^activate/([^/]*)/?\',\'index.php?pagename=activate&akey=$matches[1]\',\'top\');
        add_rewrite_rule(\'^users/$\',\'index.php?pagename=users\',\'top\');
        add_rewrite_rule(\'^users/most-active/?\',\'index.php?pagename=users&sort=mostactive\',\'top\');
        add_rewrite_rule(\'^users/most-active/paged/([^/]*)/$\',\'index.php?pagename=users&sort=mostactive&paged=$matches[1]\',\'top\');
        add_rewrite_rule(\'^users/recent/?\',\'index.php?pagename=users&sort=recent\',\'top\');
        add_rewrite_rule(\'^users/recent/paged/([^/]*)/$\',\'index.php?pagename=users&sort=recent&paged=$matches[1]\',\'top\');
        add_rewrite_rule(\'^users/oldest/?\',\'index.php?pagename=users&sort=oldest\',\'top\');
        add_rewrite_rule(\'^users/oldest/paged/([^/]*)/$\',\'index.php?pagename=users&sort=oldest&paged=$matches[1]\',\'top\');
        add_rewrite_rule(\'^users/by-username/?\',\'index.php?pagename=users&sort=byusername\',\'top\');
        add_rewrite_rule(\'^users/by-username/paged/([^/]*)/$\',\'index.php?pagename=users&sort=byusername&paged=$matches[1]\',\'top\');
问题是以下用户/最近/工作,而用户/最近/已分页/编号/不工作

有什么想法吗?

谢谢

1 个回复
最合适的回答,由SO网友:TheDeadMedic 整理而成

Your regex is a little flaky - you shouldn\'t specify the start anchor ^, and some support trailing slashes, others don\'t - plus I\'d opt for a better expression for matching page numbers. And you could certainly trim it down to:

add_rewrite_rule(
    \'activate/([^/]*)/?$\',
    \'index.php?pagename=activate&akey=$matches[1]\',\'top\' );

add_rewrite_rule(
    \'users/?$\',
    \'index.php?pagename=users\',\'top\' );

add_rewrite_rule(
    \'users/(most-active|recent|oldest|by-username)/?$\',
    \'index.php?pagename=users&sort=$matches[1]\',\'top\' );

add_rewrite_rule(
    \'users/(most-active|recent|oldest|by-username)/paged/([0-9]{1,})/?$\',
    \'index.php?pagename=users&sort=$matches[1]&paged=$matches[2]\',\'top\' );

I noticed that by-username translates to the sort parameter byusername - if the sorting logic can\'t be changed to accept the hyphenated value (which I recommend for consistency), add a separate, specific rule-pair.

If you\'re still having problems, load the request and print out $wp->matched_rule - this\'ll tell you which rule WordPress hit & proceeded to handle.

结束

相关推荐

Admin Theme customization

我遵循wordpress codex网站上关于通过插件创建管理主题的说明。我激活了插件,但我的样式表没有包含在<head>.. 这是我的代码:add_action( \'admin_init\', \'kd_plugin_admin_init\' ); add_action( \'admin_menu\', \'kd_plugin_admin_menu\' ); function kd_plugin_admin_init() { /* Register