What is the proper way to use add_rewrite_rule to remove the (question mark)?

 ̄綄美尐妖づ 提交于 2019-12-10 15:42:35

问题


I have a page called "People" with a custom template that takes in a variable called name:

example.com/people/?name=FirstLast

What I want is this:

example.com/people/FristLast

I know I need to use the add_rewrite_rule and add_rewrite_tag calls but I'm not sure how to achieve my desired result. I've tried this but I get a php error:

add_rewrite_rule('^people/([^/]*)/?','index.php?name=$matches[1]','top');
add_rewrite_tag('%name%','([^&]+)');

The error I get is: "Fatal error: Call to a member function add_rule() on a non-object"

I think I'm on the right path. I'm very knowledgeable with WP but this is my first attempt to the rewrite rule.

Thanks!


回答1:


try

add_action( 'init', 'addMyRules' );
function addMyRules(){
    add_rewrite_rule('^people/([^/]*)/?','index.php?name=$matches[1]','top');
    add_rewrite_tag('%name%','([^&]+)');
}



回答2:


You should call add_rewrite_rule in the action hook as follows:

add_action('init', 'wpa8715_intit');

function wpa8715_intit() {
    add_rewrite_rule('^people/([^/]*)/?','index.php?name=$matches[1]','top');
    add_rewrite_tag('%name%','([^&]+)');
}


来源:https://stackoverflow.com/questions/15952053/what-is-the-proper-way-to-use-add-rewrite-rule-to-remove-the-question-mark

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