wordpress - 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');
}
但它不起作用。我究竟做错了什么?
解决方案
尝试这个:
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' );
推荐阅读
- php - 升级到 Laravel Spark 6;未定义的方法
- c# - 为什么我不能使用 Process 类启动 Java?
- java - 打印数字并选择正确的 for 循环
- r - 当我在 .RMD 文件中运行 setwd 时,它不起作用
- kubernetes - 无法通过 alpha 阶段获取 kubelet 配置
- python - 从 (x,y) 的范围内随机抽样 n 位,平均到 k?
- function - 如何在每次流程块迭代后安全地清除命名函数中的变量
- gsap - 为什么我的触发器在我的 Greensock TimelineMax 场景中没有正确定位?
- angular - Angular 6 和 Material 能否在对话框中不显示整个组件,而只显示其中的一部分?
- php - 使用 php 调整 url 上的图像大小