2017-09-04 119 views
0

的網址是:WordPress的重寫URL

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

你的代碼看起來很完美,試着添加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(); 
} 
+0

感謝它的工作! – Jappan