如何在 WordPress 中创建(自定义)友好 URL?

发布于 2024-08-18 10:37:23 字数 745 浏览 12 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烟雨扶苏 2024-08-25 10:37:23

您可以使用 wordpress 的 parse_request() 函数或 mod_rewrite 来转换您的 url,但为了使其正常工作,您必须编写一个函数来捕获请求url 并将其转换为原始 url 来服务请求。

you can use both the methods parse_request() function of wordpress or mod_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.

天邊彩虹 2024-08-25 10:37:23

您可以使用重写规则,

以下是如何注册新重写规则并将其传递到 PHP 文件进行渲染的简单示例:

1.设置规则:

add_action( 'init',  function() {
    add_rewrite_rule( 'myparamname/([a-z0-9-]+)[/]?

2.刷新永久链接。转到 WP 管理 >设置>固定链接>节省。添加此代码后,这不会自动发生

3。将查询参数列入白名单:

add_filter( 'query_vars', function( $query_vars ) {
    $query_vars[] = 'myparamname';
    return $query_vars;
} );

4.添加一个处理程序以将其发送到模板文件:

add_filter( 'template_include', function( $template ) {
if ( get_query_var( 'myparamname' ) == false || get_query_var( 'myparamname' ) == '' ) {
返回$模板;
}

return get_template_directory() . '/template-name.php';

} );

请参阅:https://developer.wordpress.org/reference/functions/add_rewrite_rule /#comment-3503

, 'index.php?myparamname=$matches[1]', 'top' ); } );

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:

add_action( 'init',  function() {
    add_rewrite_rule( 'myparamname/([a-z0-9-]+)[/]?

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:

add_filter( 'query_vars', function( $query_vars ) {
    $query_vars[] = 'myparamname';
    return $query_vars;
} );

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

return get_template_directory() . '/template-name.php';

} );

See: https://developer.wordpress.org/reference/functions/add_rewrite_rule/#comment-3503

, 'index.php?myparamname=$matches[1]', 'top' ); } );

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文