使用 add_rewrite_rule 删除(问号)的正确方法是什么? [英] What is the proper way to use add_rewrite_rule to remove the (question mark)?

查看:83
本文介绍了使用 add_rewrite_rule 删除(问号)的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为People"的页面,其中包含一个自定义模板,该模板接受一个名为 name 的变量:

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

example.com/people/?name=FirstLast

example.com/people/?name=FirstLast

我想要的是这个:

example.com/people/FristLast

example.com/people/FristLast

我知道我需要使用 add_rewrite_rule 和 add_rewrite_tag 调用,但我不确定如何实现我想要的结果.我试过这个,但我收到一个 php 错误:

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%','([^&]+)');

我得到的错误是:致命错误:在非对象上调用成员函数 add_rule()"

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

我认为我走在正确的道路上.我对 WP 非常了解,但这是我第一次尝试重写规则.

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

谢谢!

推荐答案

try

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

这篇关于使用 add_rewrite_rule 删除(问号)的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆