WordPress 自定义 URL 重写

发布于 2024-09-11 02:41:16 字数 871 浏览 1 评论 0原文

我的网站中有一些链接,可将查询传递到从外部数据库查询的页面。这工作正常,例如,

mysite.com/catalog/?tl=flooring

但是我想重写此网址以显示,因为

mysite.com/catalog/flooring

我尝试修改 WordPress 中的重写规则,但它始终显示索引页面

add_filter('rewrite_rules_array','wp_insertMyRewriteRules');
add_filter('query_vars','wp_insertMyRewriteQueryVars');
add_filter('init','flushRules');

// Remember to flush_rules() when adding rules
function flushRules(){
    global $wp_rewrite;
    $wp_rewrite->flush_rules();
}

// Adding a new rule
function wp_insertMyRewriteRules($rules)
{
    $newrules = array();
    $newrules['(catalog)/([a-zA-Z0-9 ]+)$'] = '/catalog/?tl=$matches[2]';
    return $newrules + $rules;
}

// Adding the id var so that WP recognizes it
function wp_insertMyRewriteQueryVars($vars)
{
    array_push($vars, 'id');
    return $vars;
}

I have links in my site that pass queries to pages that query from an external database. This works fine e.g.

mysite.com/catalog/?tl=flooring

however i want to rewrite this url to appear as

mysite.com/catalog/flooring

Ive tried modifying the rewrite rules in wordpress but it always displays the index page

add_filter('rewrite_rules_array','wp_insertMyRewriteRules');
add_filter('query_vars','wp_insertMyRewriteQueryVars');
add_filter('init','flushRules');

// Remember to flush_rules() when adding rules
function flushRules(){
    global $wp_rewrite;
    $wp_rewrite->flush_rules();
}

// Adding a new rule
function wp_insertMyRewriteRules($rules)
{
    $newrules = array();
    $newrules['(catalog)/([a-zA-Z0-9 ]+)
] = '/catalog/?tl=$matches[2]';
    return $newrules + $rules;
}

// Adding the id var so that WP recognizes it
function wp_insertMyRewriteQueryVars($vars)
{
    array_push($vars, 'id');
    return $vars;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

指尖上得阳光 2024-09-18 02:41:16

WordPress 中的重写规则并不完全像您期望的那样工作。所有重写规则都映射到文件处理程序(几乎总是 index.php),而不是另一个 URL。

这是一个简短的代码示例;

$rules['catalog/(.*)/?'] = 'index.php?pagename=catalog&tl=$matches[1]';
array_push($query_vars, 'tl'); // note query var should match your query string

这会将 catalog/whatever 映射到 WordPress 页面“catalog”,并传递查询变量“tl”。您可以为“catalog”创建页面模板,然后使用 get_query_var('tl') 获取“tl”的值。

还要避免使用像 id 这样的查询变量 - 使用更独特的东西(例如“tl”)以避免与 WordPress 发生冲突。

并且不要在每次初始化时刷新规则!这是不好的做法,并且会写入 .htaccess 并在每个页面加载时调用数据库更新!

每当您更新帖子或永久链接结构时,WordPress 将始终刷新永久链接(只需在更改代码时更新您的永久链接)。

Rewrite rules in WordPress don't quite work like how you're expecting them to. All rewrite rules map to a file handler (almost always index.php), not another URL.

Here is a shortened code example;

$rules['catalog/(.*)/?'] = 'index.php?pagename=catalog&tl=$matches[1]';
array_push($query_vars, 'tl'); // note query var should match your query string

This would map catalog/whatever to the WordPress page 'catalog', and pass along the query var 'tl'. You could create a page template for 'catalog', then pick up the value of 'tl' using get_query_var('tl').

Also avoid using query vars like id - use something more unique (like 'tl') to avoid clashes with WordPress.

And don't flush rules on every init! It's bad practice, and will write to .htaccess and call database updates on every page load!

WordPress will always flush permalinks whenever you update a post or your permalink structure (simply update your permalinks when you make changes to your code).

月牙弯弯 2024-09-18 02:41:16

本文来自 Raazorlight更详细地介绍 WP 中获取查询字符串和 URL 重写的过程。

另外,请检查那里的注释,了解如果重新保存永久链接,为什么可能不需要调用flushRules()。 (可选)flushRules() 可以在插件激活回调期间仅调用一次。

深入挖掘后,评论者“pmdci”链接到一个关于使用自定义分类法将查询传递给自定义帖子类型的相关主题的指导性帖子/传奇。

This article over at Raazorlight goes into a bit more detail on the process of grabbing querystrings and URL rewriting in WP.

Also, check the comments there to see why calling flushRules() may not be necessary if you re-save permalinks. Optionally, flushRules() can be called just once during plugin activation callback.

Digging deeper, commenter 'pmdci' links over to an instructive post/saga on the related topic of passing a query to a custom post type using a custom taxonomy.

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