worpdress页面带自定义参数伪静态设置

找来很久,找到了以下伪静态设置方法,适用于带自定义参数页面的伪静态!如/a?id=页面

在function.php中加入以下代码

add_filter( 'rewrite_rules_array','my_insert_rewrite_rules' );
add_filter( 'query_vars','my_insert_query_vars' );
add_action( 'wp_loaded','my_flush_rules' );

// flush_rules() if our rules are not yet included
function my_flush_rules(){
$rules = get_option( 'rewrite_rules' );

if ( ! isset( $rules['a-(.*).html$'] ) ) {
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
}

// Adding a new rule
function my_insert_rewrite_rules( $rules )
{
$newrules = array();
$newrules['a-(.*).html$'] = 'index.php?pagename=a&h=$matches[1]';
return $newrules + $rules;
}

function my_insert_query_vars( $vars )
{
array_push($vars, 'h');
return $vars;
}

调用ID

<? echo $wp_query->query_vars['h']; ?>
End:感谢阅读,感谢打赏!

你也来说两句呗

电子邮件地址不会被公开。 必填项已用*标注