首页 > 解决方案 > WP自定义重写规则

问题描述

我有网址: http ://example.com/alternatywy/?id=getresponse

我正在尝试将其重写为: http ://example.com/alternatywy/getresponse

我已经添加到我的functions.php:

add_action('init', 'dcc_rewrite_tags');
function dcc_rewrite_tags() {
    add_rewrite_tag('%id%', '([^&]+)');
}

add_action('init', 'dcc_rewrite_rules');
function dcc_rewrite_rules() {
    add_rewrite_rule('^alternatywy/(.+)/?$','index.php?page_id=8286&id=$matches[1]','top');
}

但它不起作用。我究竟做错了什么?

标签: wordpressurl-rewriting

解决方案


尝试这个:

add_filter('query_vars', function($vars) {
    $vars[] = "id";
    return $vars;
});

add_action('init', 'dcc_rewrite_rules');
function dcc_rewrite_rules() {
    add_rewrite_rule('^alternatywy/([^/]+)/?$','index.php?page_id=8286&id=$matches[1]','top');
}

现在是刷新重写规则的时候了。您可以通过代码或手动保存永久链接以两种方式完成。

手动的:

从 WordPress 管理屏幕中,选择设置 -> 永久链接,然后单击保存更改而不进行任何更改。

代码:

/* Flush rewrite rules for custom post types. */
add_action( 'after_switch_theme', 'flush_rewrite_rules' );

推荐阅读