Wordpress rewrite url not working on custom template

白昼怎懂夜的黑 提交于 2019-12-25 11:57:48

问题


The URL is:

mywebsite.com/custom/?var1=random_text

and I want

mywebsite.com/custom/random_text

here's the code in my functions.php file:

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'
    );
}

But it still returns 404 error. What am I doing wrong?


回答1:


Your code is looking perfect try to add flush_rewrite_rules function.

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();
}


来源:https://stackoverflow.com/questions/46031990/wordpress-rewrite-url-not-working-on-custom-template

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!