网址为:
mywebsite.com/custom/?var1=random_text
我想要
mywebsite.com/custom/random_text
这是我的functions.php文件中的代码:
add_filter( 'query_vars', 'wpse12965_query_vars' );
function wpse12965_query_vars( $query_vars )
{
$query_vars[] = 'var1';
return $query_vars;
}
add_action( 'init', 'wpse12065_init' );
function wpse12065_init()
{
add_rewrite_rule(
'custom(/([^/]+))?/?',
'index.php?pagename=custom&var1=$matches[1]',
'top'
);
}
但它仍然会返回404错误。我做错了什么?
答案 0 :(得分:0)
您的代码看起来很完美,尝试添加flush_rewrite_rules函数。
add_filter( 'query_vars', 'wpse12965_query_vars' );
function wpse12965_query_vars( $query_vars )
{
$query_vars[] = 'var1';
return $query_vars;
}
add_action( 'init', 'wpse12065_init' );
function wpse12065_init(){
add_rewrite_rule(
'custom(/([^/]+))?/?',
'index.php?pagename=custom&var1=$matches[1]',
'top'
);
flush_rewrite_rules();
}