wordpress - 将 url 查询字符串转换为静态 [英] wordpress - Convert url query string to static

查看:51
本文介绍了wordpress - 将 url 查询字符串转换为静态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以帮助我了解 wordpress 中是否有任何标准函数或流程可用于将动态查询字符串转换为静态,就像 wordpress 在 wp-admin 中设置永久链接时如何将动态网址转换为 SEF 网址一样.

Can anyone please help me understand if there is any standard function or process in wordpress that can be used to convert dynamic query strings to static just like how wordpress converts dynamic urls into SEF urls on setting permalinks in wp-admin.

我正在开发一个基于 woocommerce 的插件,其中将向用户提供动态链接.这会导致另一个页面,其中使用 $_GET 提取查询字符串.

I am developing a plugin based on woocommerce where the user will be provided with a dynamic link. This leads to another page where the query string is extracted using $_GET.

现在我希望在 url 中没有动态格式但静态/SEF 格式的情况下实现这一点.

Now I am looking to make this happen without dynamic format in url but static/SEFformat.

更新我的问题:

以下是我一直在我的插件中尝试的代码,但无法看到 url 格式的更改.

Below is the code I have been trying in my plugin and not able to see the change the url format.

function dashboard_messages_url_rewrite() {
global $wp_rewrite;
add_rewrite_tag('%cid%', '([^&]+)');
add_rewrite_rule('^conversations/([0-9]+)/', 'index.php?page_id=3242&cid=$matches[1]', 'top');

$wp_rewrite->flush_rules(true);
}
add_action('init', 'dashboard_messages_url_rewrite', 10, 0);

例如,如果 url 生成为

for example if the url is generated as

http://example.com/conversations/?cid=2234

我希望它被 wordpress 转换为如下

I want it to be converted by wordpress as below

http://example.com/conversations/2234

希望这有助于理解我一直在尝试的内容.谢谢

Hope this will help to understand what I have been trying. Thanks

推荐答案

您的规则缺少最后的 ? 使最后一个斜杠成为可选.添加问号后,您的重定向应该可以工作.函数调用将如下所示:

Your rule is missing the final ? to make the last slash optional. After adding the question mark, your redirect should work. The function call will look like this:

add_rewrite_rule('^conversations/([0-9]+)/?', 'index.php?page_id=3242&cid=$matches[1]', 'top');

此外,每次您在 Wordpress 中以编程方式创建新的重写规则、使用重写函数或添加自定义帖子类型时,例如,您都需要刷新永久链接.为此,只需转到 WP 管理员中的永久链接设置页面,然后点击保存按钮.这足以刷新规则.

Also, every time you programmatically create new rewrite rules in Wordpress, using rewrite functions or when adding custom post types, for instance, you need to flush the permalinks. To do so, simply go to permalink settings page in your WP admin and hit the save button. This will be enough to flush the rules.

这篇关于wordpress - 将 url 查询字符串转换为静态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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