如何在 WordPress 中创建(自定义)友好 URL?
在 WordPress 中,我设置了如下所示的永久链接结构: /%category%/%postname%
创建自定义搜索页面后,我现在拥有以下 URL:http://mypage.com/search?foo1=bar&foo2=&foo3=&foo4=
我有两个问题:
1)如何转换这个网址,以便我到达例如http://mypage.com/search/foo1/bar
?
2)有没有办法删除“未使用”的参数? (&foo2=&foo3=&foo4=
)
我发现这篇文章,将我指向 parse_request 函数Wordpress,以及这篇文章谈论 mod_rewrite。但不太确定如何进行,或者使用什么方法更好。
In Wordpress, I have set the permalink structure like this: /%category%/%postname%
After creating a custom search page, I now have the following URL:http://mypage.com/search?foo1=bar&foo2=&foo3=&foo4=
I have two questions:
1) How can I transform this url so that I get to e.g. http://mypage.com/search/foo1/bar
?
2) Is there a way I can remove the "unused" parameters? (&foo2=&foo3=&foo4=
)
I found this post, pointing me to parse_request function in Wordpress, and this post talking about mod_rewrite. But not quite sure how to proceed, or what method is better to use.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 wordpress 的
parse_request()
函数或mod_rewrite
来转换您的 url,但为了使其正常工作,您必须编写一个函数来捕获请求url 并将其转换为原始 url 来服务请求。you can use both the methods
parse_request()
function of wordpress ormod_rewrite
for transforming your url, but for that to work properly you will have to write a function that catches the requested url and translates it into original url to serve the request.您可以使用重写规则,
以下是如何注册新重写规则并将其传递到 PHP 文件进行渲染的简单示例:
1.设置规则:
2.刷新永久链接。转到 WP 管理 >设置>固定链接>节省。添加此代码后,这不会自动发生
3。将查询参数列入白名单:
4.添加一个处理程序以将其发送到模板文件:
add_filter( 'template_include', function( $template ) {
if ( get_query_var( 'myparamname' ) == false || get_query_var( 'myparamname' ) == '' ) {
返回$模板;
}
} );
请参阅:https://developer.wordpress.org/reference/functions/add_rewrite_rule /#comment-3503
You can use rewrite rules,
Here is a simple example of how to register a new rewrite rule, and pass it off to a PHP file for rendering:
1. Setup a rule:
2. Flush permalinks. Go to WP Admin > Settings > Permalinks > Save. This doesn’t happen automatically after you add this code
3. Whitelist the query param:
4. Add a handler to send it off to a template file:
add_filter( 'template_include', function( $template ) {
if ( get_query_var( 'myparamname' ) == false || get_query_var( 'myparamname' ) == '' ) {
return $template;
}
} );
See: https://developer.wordpress.org/reference/functions/add_rewrite_rule/#comment-3503