我正在为我的插件制作SEF urls。逻辑是:如果页面上有my_shortcode,那么将url的其余部分作为parametr dir=传递--这段代码可以很好地工作在我的wordpress安装中,而不是在另一个安装上。我得到404。有什么不对的吗?
add_action('init', array( $this, 'do_rewrite' ));
public function do_rewrite()
{
$page = get_page_by_path($_SERVER['REQUEST_URI']);
if (! isset($page->ID)) {
preg_match('#^/?(.*?)/(.+)/?$#', $_SERVER['REQUEST_URI'], $matches);
$parent_page = get_page_by_path($matches[1]);
if ($parent_page) {
$content = $parent_page->post_content;
if (preg_match('/\[my_shotrcode(.*?)\]/i', $content)) {
add_rewrite_rule('^/?(.*?)/(.+)/?附注:重定向规则更新:谁能解释我发生了什么事?我评论了这2条如果: // if ($parent_page) {
// if (preg_match('/\[my_shotrcode(.*?)\]/i', $content)) {重定向开始工作。然后我把它还给了..。而且它一直在工作!, 'index.php?pagename=$matches[1]&dir=$matches[2]', 'top');
add_rewrite_tag('%dir%', '([^&]+)');
}
}
}
}附注:重定向规则
更新:谁能解释我发生了什么事?我评论了这2条如果:
A1
重定向开始工作。然后我把它还给了..。而且它一直在工作!
发布于 2020-11-09 15:31:41
看来问题出在冲洗规则上。设置中的刷新规则--Permalinks在某种程度上没有帮助,所以我在下面的代码中这样做:
if (! in_array('index.php?pagename=$matches[1]&dir=$matches[2]', get_option('rewrite_rules'))) {
flush_rewrite_rules();
}https://wordpress.stackexchange.com/questions/377857
复制相似问题